Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

PUT ALL THE PIECES TOGETHER

  • Loading branch information...
commit fdb491dba87d7d90f12c2ab73b9c3a1e2e7d9133 1 parent fecb47b
@progrium authored
View
1  .gitignore
@@ -3,3 +3,4 @@ build
TODO
*.pyc
**/*.pyc
+dist
View
4 setup.py
@@ -12,7 +12,9 @@
description='Magic pipe in the sky',
packages=find_packages(),
install_requires=['pyzmq-static', 'argparse', 'requests', 'colorama'],
- data_files=[],
+ zip_safe=False,
+ package_data={
+ 'skypipe': ['satellite/*']},
entry_points={
'console_scripts': [
'skypipe = skypipe.client:run',]}
View
7 skypipe/client.py
@@ -2,6 +2,8 @@
import sys
import zmq
+from . import deploy
+
SP_HEADER = "SKYPIPE/0.1"
SP_CMD_HELLO = "HELLO"
SP_CMD_DATA = "DATA"
@@ -9,7 +11,6 @@
SP_CMD_UNLISTEN = "UNLISTEN"
SP_DATA_EOF = ""
-zmq_endpoint = os.environ.get("ZMQ_ENDPOINT", "tcp://0.0.0.0:9000")
try:
pipe_name = sys.argv[1]
except IndexError:
@@ -18,6 +19,10 @@
def run():
context = zmq.Context()
try:
+ zmq_endpoint = os.environ.get("ZMQ_ENDPOINT")
+ if not zmq_endpoint:
+ zmq_endpoint = deploy.find_satellite(context)
+
if os.isatty(0):
# output mode
socket = context.socket(zmq.DEALER)
View
143 skypipe/deploy.py
@@ -0,0 +1,143 @@
+import time
+import os
+import os.path
+import socket
+import subprocess
+
+import zmq
+
+import dotcloud.ui.cli
+from dotcloud.ui.cli import CLI
+from dotcloud.ui.config import GlobalConfig, CLIENT_KEY, CLIENT_SECRET
+from dotcloud.client import RESTClient
+from dotcloud.client.auth import NullAuth
+from dotcloud.client.errors import RESTAPIError
+
+url = os.environ.get('DOTCLOUD_API_ENDPOINT', 'https://api-experimental.dotcloud.com/v1')
+app = "skypipe"
+satellite_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'satellite')
+
+class FakeSubprocess(object):
+ @staticmethod
+ def call(*args, **kwargs):
+ kwargs['stdout'] = subprocess.PIPE
+ return subprocess.call(*args, **kwargs)
+dotcloud.ui.cli.subprocess = FakeSubprocess
+
+def setup_account(cli):
+ client = RESTClient(endpoint=cli.client.endpoint)
+ client.authenticator = NullAuth()
+ urlmap = client.get('/auth/discovery').item
+ username = cli.prompt('dotCloud username')
+ password = cli.prompt('Password', noecho=True)
+ credential = {'token_url': urlmap.get('token'),
+ 'key': CLIENT_KEY, 'secret': CLIENT_SECRET}
+ try:
+ token = cli.authorize_client(urlmap.get('token'), credential, username, password)
+ except Exception as e:
+ cli.die('Username and password do not match. Try again.')
+ token['url'] = credential['token_url']
+ config = GlobalConfig()
+ config.data = {'token': token}
+ config.save()
+ cli.global_config = GlobalConfig() # reload
+ cli.setup_auth()
+ cli.get_keys()
+
+def check_satellite(cli, context):
+ url = '/applications/{0}/environment'.format(app)
+ try:
+ var = cli.user.get(url).item
+ port = var['DOTCLOUD_SATELLITE_ZMQ_PORT']
+ host = socket.gethostbyname(var['DOTCLOUD_SATELLITE_ZMQ_HOST'])
+ endpoint = "tcp://{}:{}".format(host, port)
+ req = context.socket(zmq.DEALER)
+ req.linger = 0
+ req.connect(endpoint)
+ req.send_multipart(["SKYPIPE/0.1", "HELLO"])
+ ok = None
+ timeout = time.time() + 20
+ while time.time() < timeout:
+ try:
+ ok = req.recv_multipart(zmq.NOBLOCK)
+ break
+ except zmq.ZMQError:
+ continue
+ req.close()
+ if ok:
+ cli.info("DEBUG: Found satellite")
+ return endpoint
+ else:
+ #cli.die("no reply")
+ return deploy_satellite(cli)
+ except (RESTAPIError, KeyError):
+ #cli.die("key error")
+ return deploy_satellite(cli)
+
+def deploy_satellite(cli, context):
+ cli.info(' '.join(["Launching skypipe satellite.",
+ "This only has to happen once for your account."]))
+ cli.info("This may take about a minute...")
+ # destroy
+ url = '/applications/{0}'.format(app)
+ try:
+ res = cli.user.delete(url)
+ except RESTAPIError:
+ pass
+
+ # create
+ url = '/applications'
+ try:
+ cli.user.post(url, {
+ 'name': app,
+ 'flavor': 'sandbox'
+ })
+ except RESTAPIError as e:
+ if e.code == 409:
+ cli.die('Application "{0}" already exists.'.format(app))
+ else:
+ cli.die('Creating application "{0}" failed: {1}'.format(app, e))
+ class args: application = app
+ #cli._connect(args)
+
+ # push
+ protocol = 'rsync'
+ url = '/applications/{0}/push-endpoints{1}'.format(app, '')
+ endpoint = cli._select_endpoint(cli.user.get(url).items, protocol)
+ class args: path = satellite_path
+ cli.push_with_rsync(args, endpoint)
+
+ # deploy
+ revision = None
+ clean = False
+ url = '/applications/{0}/deployments'.format(app)
+ response = cli.user.post(url, {'revision': revision, 'clean': clean})
+ deploy_trace_id = response.trace_id
+ deploy_id = response.item['deploy_id']
+
+ try:
+ res = cli._stream_deploy_logs(app, deploy_id,
+ deploy_trace_id=deploy_trace_id, follow=True)
+ if res != 0:
+ return res
+ except KeyboardInterrupt:
+ cli.error('You\'ve closed your log stream with Ctrl-C, ' \
+ 'but the deployment is still running in the background.')
+ cli.error('If you aborted because of an error ' \
+ '(e.g. the deployment got stuck), please e-mail\n' \
+ 'support@dotcloud.com and mention this trace ID: {0}'
+ .format(deploy_trace_id))
+ cli.error('If you want to continue following your deployment, ' \
+ 'try:\n{0}'.format(
+ cli._fmt_deploy_logs_command(deploy_id)))
+ cli.die()
+ except RuntimeError:
+ pass
+ return check_satellite(cli, context)
+
+def find_satellite(context):
+ cli = CLI(endpoint=url)
+ if not cli.global_config.loaded:
+ cli.info("First time use, please setup dotCloud account")
+ setup_account(cli)
+ return check_satellite(cli, context)
View
4 satellite/builder → skypipe/satellite/builder
@@ -4,8 +4,8 @@ cd
(cat <<-EOF
virtualenv --python=python2.7 env
. env/bin/activate
- python setup.py install
- python satellite/server.py
+ pip install -r requirements.txt
+ python server.py
EOF
) > run
chmod a+x run
View
5 skypipe/satellite/dotcloud.yml
@@ -0,0 +1,5 @@
+satellite:
+ type: custom
+ buildscript: builder
+ ports:
+ zmq: tcp
View
1  skypipe/satellite/requirements.txt
@@ -0,0 +1 @@
+pyzmq-static
View
0  satellite/server.py → skypipe/satellite/server.py
File renamed without changes

0 comments on commit fdb491d

Please sign in to comment.
Something went wrong with that request. Please try again.