Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setup.py python package #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ images.fortests
.coverage
attic
.idea/
.vscode/
8 changes: 8 additions & 0 deletions Dockerfile.rackattack-api-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM rackattack-nas.dc1:5000/ubuntu-dev-base:379308aa77bd0a19ebf12d868e7d19ee35a19d6c
RUN apt-get install -y python2.7 python2.7-dev
RUN curl 'https://bootstrap.pypa.io/pip/2.7/get-pip.py' | python2.7
COPY requirements.txt /tmp/requirements.txt
COPY dev-requirements.txt /tmp/dev-requirements.txt
RUN python2.7 -m pip install -r /tmp/requirements.txt -r /tmp/dev-requirements.txt
RUN python3 -m pip install -r /tmp/requirements.txt -r /tmp/dev-requirements.txt
RUN rm /tmp/requirements.txt /tmp/dev-requirements.txt
36 changes: 30 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
all: check_convention unittest
.PHONY: all test build test_py2 test_py3 lint_py2 lint_py3
ARTIFACT=dist/racattack_api-*-py2.py3-none-any.whl
all: lint test build

unittest:
UPSETO_JOIN_PYTHON_NAMESPACES=Yes PYTHONPATH=py python -m coverage run py/rackattack/tests/runner.py
python -m coverage report --show-missing --include=py/*.py --omit=py/rackattack/tests/*.py,*/__init__.py
test: test_py2 test_py3

check_convention:
pep8 py test* --max-line-length=109
test_py3:
PYTHONPATH=py python3 -m coverage run py/rackattack/tests/runner.py
python3 -m coverage report --show-missing --include=py/*.py --omit=py/rackattack/tests/*.py,*/__init__.py

test_py2:
PYTHONPATH=py python2.7 -m coverage run py/rackattack/tests/runner.py
python2.7 -m coverage report --show-missing --include=py/*.py --omit=py/rackattack/tests/*.py,*/__init__.py

lint: lint_py2 lint_py3

lint_py3:
python3 -m pep8 py --max-line-length=120

lint_py2:
python2.7 -m pep8 py --max-line-length=120

build: $(ARTIFACT)

clean:
find . -name *.pyc -delete
find . -name __pycache__ -delete
rm -rf dist */*.egg-info build *.stratolog

$(ARTIFACT):
python3 -m build --wheel

#this can not run in dirbalak clean build, as solvent can not yet be run at this point (still running in
#rootfs-build-nostrato, this project is a dependency of rootfs-buid). This is why this is actually being
#run in pyracktest
delayed_racktest:
UPSETO_JOIN_PYTHON_NAMESPACES=yes PYTHONPATH=$(PWD):$(PWD)/py python test/test.py $(TESTS)
PYTHONPATH=$(PWD):$(PWD)/py python test/test.py $(TESTS)
virttest:
RACKATTACK_PROVIDER=tcp://localhost:1014@@amqp://guest:guest@localhost:1013/%2F@@http://localhost:1016 $(MAKE) delayed_racktest
phystest:
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Rackattack API
API for provisioning rackattack hosts


```python
from rackattack.ssh import connection

def get_connection(ip, username, password, timeout=5 * 60):
node = connection.Connection(ip, username, password)
node.waitForTCPServer(timeout=timeout, interval=60)
node.connect()
return node

node = get_connection("192.168.1.1", "root", "password")
date = node.run.script("date")

```
2 changes: 2 additions & 0 deletions artifacts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pips:
- dist/rackattack-api-*-py2.py3-none-any.whl
5 changes: 5 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pep8
coverage
ipdb
build
mock
2 changes: 0 additions & 2 deletions py/rackattack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
import upseto.pythonnamespacejoin
__path__.extend(upseto.pythonnamespacejoin.join(globals()))
4 changes: 2 additions & 2 deletions py/rackattack/packagesvalidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def _validateMinimumVersion(packageName, minVersion):
package = __import__(packageName)
actualVersion = package.__version__
actualVersion = _normalizedVersionNumber(actualVersion)
for componentNr in xrange(len(minVersion)):
for componentNr in range(len(minVersion)):
minimumComponent = minVersion[componentNr]
actualComponent = actualVersion[componentNr]
if actualComponent < minimumComponent:
Expand All @@ -32,5 +32,5 @@ def _validateMinimumVersion(packageName, minVersion):


def validateMinimumVersions(**packagesVersions):
for packageName, minVersion in packagesVersions.iteritems():
for packageName, minVersion in packagesVersions.items():
_validateMinimumVersion(packageName, minVersion)
2 changes: 1 addition & 1 deletion py/rackattack/pikapatchwakeupfromanotherthread.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import Queue
from queue import Queue
import select
import logging
import signal
Expand Down
20 changes: 10 additions & 10 deletions py/rackattack/playaround.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
parser.add_argument("--disk1SizeGB", type=int, default=None)
args = parser.parse_args()

print "Connecting"
print("Connecting")
client = clientfactory.factory()
print "Allocating"
print("Allocating")
hardwareConstraints = dict()
if args.disk1SizeGB is not None:
hardwareConstraints['disk1SizeGB'] = args.disk1SizeGB
print "Hardware Constraints:", hardwareConstraints
print("Hardware Constraints:"), hardwareConstraints
requirement = api.Requirement(
imageLabel=args.label,
imageHint="playaround",
Expand All @@ -30,13 +30,13 @@
allocationInfo=api.AllocationInfo(user=args.user, purpose="playaround", nice=args.nice))
allocation.wait(timeout=8 * 60)
assert allocation.done(), "Allocation failed"
print "Done allocating, Waiting for boot to finish"
print("Done allocating, Waiting for boot to finish")
try:
node = allocation.nodes()['node']
credentials = dict(port=22)
credentials.update(node.rootSSHCredentials())
print "ROOT SSH Credentials:"
print credentials
print("ROOT SSH Credentials:")
print(credentials)
if not args.noSSH:
ssh = connection.Connection(**credentials)
ssh.waitForTCPServer()
Expand All @@ -51,17 +51,17 @@
try:
log = node.fetchSerialLog()
open("/tmp/serial.log", "w").write(log)
print "serial log stored in /tmp/serial.log"
print("serial log stored in /tmp/serial.log")
except Exception as e:
print "Unable to fetch serial log: %s" % e
print("Unable to fetch serial log: %s" % e)
raise
print "Opening ssh session. Close it to free up allocation"
print("Opening ssh session. Close it to free up allocation")
os.system(
"sshpass -p %(password)s ssh -o ServerAliveInterval=5 -o ServerAliveCountMax=1 "
"-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p %(port)d "
"%(username)s@%(hostname)s" % credentials)
else:
print "Not connecting to machine via SSH. Hit Ctrl-C to close the machine."
print("Not connecting to machine via SSH. Hit Ctrl-C to close the machine.")
time.sleep(1000000000)
finally:
allocation.free()
3 changes: 2 additions & 1 deletion py/rackattack/ssh/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def execute(self, command, outputTimeout=20 * 60, wrapCmd=True, verbose=True, co
try:
if verbose:
name = getattr(self._sshClient, 'name', '')
self._logger.debug("Running bash script: %(cmd)s %(name)s" % dict(cmd=command.strip(), name='on ' + name if name else name))
message = "Running bash script: %s %s" % (command.strip(), "on " + name if name else "")
self._logger.debug(message)
chan.exec_command(commandToExecute)
chan.settimeout(outputTimeout)
stdin = chan.makefile('wb', -1)
Expand Down
2 changes: 1 addition & 1 deletion py/rackattack/tcp/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,4 @@ def _refetchInauguratorIDs(self):

def renameUser(self, user):
assert isinstance(user, str)
self._ipcClient.call('rename_allocation', id=self._id, user=user)
self._ipcClient.call('rename_allocation', id=self._id, user=user)
2 changes: 1 addition & 1 deletion py/rackattack/tcp/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pika
import simplejson
import threading
import Queue
from queue import Queue


class PublishSpooler(threading.Thread):
Expand Down
2 changes: 1 addition & 1 deletion py/rackattack/tests/fake_pika.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pika
import Queue
from queue import Queue


_rabbitMQBrokers = dict()
Expand Down
2 changes: 1 addition & 1 deletion py/rackattack/tests/test_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import mock
import json
import pika
import Queue
from queue import Queue
import logging
import greenlet
import unittest
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools", "python-setuptools-plugins", "wheel"]
build-backend = "setuptools.build_meta"
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
python-setuptools-plugins==0.0.2+g865898c
twisted
greenlet
28 changes: 28 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from setuptools_plugins.version import get_version
import setuptools

with open("README.md", "r") as fh:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fh to something more meaningful for readability

long_description = fh.read()

setuptools.setup(
name="rackattack_api",
packages=setuptools.find_packages(where="py"),
package_dir={"": "py"},
setup_requires=['python-setuptools-plugins'],
version=get_version(),
author="Stratoscale",
author_email="[email protected]",
description="API for provisioning rackattack hosts",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/Stratoscale/rackattack-api",
project_urls={
"Bug Tracker": "https://github.com/Stratoscale/rackattack-api/issues",
},
classifiers=[
"Programming Language :: Python :: 2",
"License :: Apache License 2.0",
"Operating System :: OS Independent",
],
options={'bdist_wheel': {'universal': True}}
)
5 changes: 5 additions & 0 deletions skipper.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
registry: rackattack-nas.dc1:5000
build-container-image: rackattack-api-build

make:
makefile: Makefile
10 changes: 5 additions & 5 deletions test/simpleusecase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from rackattack import clientfactory
from rackattack.ssh import connection
from rackattack import api
import pdb
import ipdb


hint = 'rootfs-basic'
Expand All @@ -15,18 +15,18 @@
requirement = api.Requirement(imageLabel=label, imageHint=hint)
info = api.AllocationInfo(user='rackattack-api test', purpose='integration test')
allocation = client.allocate(dict(it=requirement), info)
print "Created allocation, waiting for node inauguration"
print("Created allocation, waiting for node inauguration")
allocation.wait(timeout=7 * 60)
print "Allocation successfull, waiting for ssh"
print("Allocation successfull, waiting for ssh")
try:
nodes = allocation.nodes()
assert len(nodes) == 1, nodes
it = nodes['it']
ssh = connection.Connection(**it.rootSSHCredentials())
ssh.waitForTCPServer()
ssh.connect()
print "SSH connected"
pdb.set_trace()
print("SSH connected")
ipdb.set_trace()
echo = ssh.run.script("echo hello")
assert 'hello' in echo, echo
finally:
Expand Down
46 changes: 23 additions & 23 deletions test/test.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import logging
import unittest
import subprocess
import os
import test
import time
import contextlib
import socket
import threading
import thread
from rackattack import clientfactory
from rackattack.ssh import connection
from rackattack import api
from rackattack import clientfactory
from rackattack.tcp import transport
from rackattack.tcp import subscribe
from rackattack import api
import contextlib
import subprocess
import threading
import unittest
import logging
import thread
import socket
import time
import test
import os


SIMPLE_USE_CASE = os.path.join(os.path.dirname(test.__file__), "simpleusecase.py")
Expand All @@ -39,9 +39,9 @@ def test_singleNodeAllocation(self):

def test_singleNodeAllocation_PDBDoesNotCauseAllocationToDie(self):
popen = subprocess.Popen(["python", SIMPLE_USE_CASE], stdin=subprocess.PIPE)
print "Sleeping for 180 seconds, to make sure heartbeat timeout occours, if pdb stops"
print("Sleeping for 180 seconds, to make sure heartbeat timeout occours, if pdb stops")
time.sleep(180)
print "Done Sleeping for 180 seconds, resuming PDB"
print("Done Sleeping for 180 seconds, resuming PDB")
popen.stdin.write("c\n")
popen.stdin.close()
result = popen.wait()
Expand All @@ -65,18 +65,18 @@ def test_LocalForwardingTunnel(self):
ssh.run.backgroundScript("python /tmp/server.py 7789 > /tmp/output")
self._send(port2, "wassup")
self.assertIn('wassup', ssh.ftp.getContents("/tmp/output"))
print "Closing just one tunnel server"
print("Closing just one tunnel server")
ssh.tunnel.stopLocalForward(7787)
time.sleep(1)
print "Stopping all tunnels"
print("Stopping all tunnels")
ssh.tunnel.stopAll()
with self.assertRaises(Exception):
self._receiveAll(port)
print "Closing tunnel"
print("Closing tunnel")
ssh.tunnel.close()
print "Sleeping for few seconds, watch for exceptions from other threads"
print("Sleeping for few seconds, watch for exceptions from other threads")
time.sleep(3)
print "Done Sleeping for few seconds"
print("Done Sleeping for few seconds")

def _flood_server(self, port, message):
s = socket.socket()
Expand Down Expand Up @@ -114,20 +114,20 @@ def _allocateOne(self):
requirement = api.Requirement(imageLabel=LABEL, imageHint=HINT)
info = api.AllocationInfo(user='rackattack-api test', purpose='integration test')
allocation = client.allocate(dict(it=requirement), info)
print "Created allocation, waiting for node inauguration"
print("Created allocation, waiting for node inauguration")
try:
allocation.wait(timeout=7 * 60)
print "Allocation successfull, waiting for ssh"
print("Allocation successfull, waiting for ssh")
nodes = allocation.nodes()
assert len(nodes) == 1, nodes
it = nodes['it']
it.fetchSerialLog()
allocation.fetchPostMortemPack()
print "SSH credentials:", it.rootSSHCredentials()
print("SSH credentials:", it.rootSSHCredentials())
ssh = connection.Connection(**it.rootSSHCredentials())
ssh.waitForTCPServer()
ssh.connect()
print "SSH connected"
print("SSH connected")
yield it, ssh, allocation
finally:
allocation.free()
Expand Down