def handle(self, **options): client = self.get_client(options) # First, see if we have an ssh public key public_key = self.get_public_key() if "Windows" in platform.platform(): self.win_setup_ssh() if not public_key: self.make_key() public_key = self.get_public_key() if not public_key: raise CommandError("Unknown error making SSH key.") if "Windows" in platform.platform(): self.win_write_keyinfo() response, content = client.post( '/account/sshkeys/', { "key": public_key, }, ) if response.status in (200, 302): if content and "form" in content and "errors" in content["form"]: print content['form']['errors']['key'][0] else: print "Added SSH key." else: raise CommandError("Unknown error, %s: %s" % (response.status, content))
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Usage: epio set_app appname") try: fh = open(".epio-app", "w") fh.write(args[0]) fh.close() except IOError: raise CommandError( "Could not write .epio-app file. Please check your permissions." )
def handle_app(self, app, **options): client = self.get_client(options) # Get the current processes response, content = client.get('app/%s/processes/' % app) if response.status == 200: print "%-10s %-30s %-15s %-15s %s" % ( "STATE", "LOCATION", "MEMORY", "TYPE", "COMMAND") for type, instances in content['processes'].items(): for host, port, state, extra in instances: if host.endswith(".ep.io"): host = host[:-6] print "%-10s %-30s %-15s %-15s %s" % ( state, "%s:%s" % (host, port), "%.1fMB/%iMB" % ( extra.get("memory_usage", 0), extra.get("memory_limit", 0), ), "[%s]" % type, extra.get("command_line", "unknown"), ) else: raise CommandError("Unknown error, %s: %s" % (response.status, content))
def handle_app_name(self, app_name, arg_app_name=None, **options): client = self.get_client(options) # Is there already a file here? if os.path.exists(".epio-app"): print "This directory is already associated with the app '%s'" % open(".epio-app").read().strip() print "Please remove the .epio-app file if you wish to unassociate it." sys.exit(1) # If there's no app name passed in via an option, try the first argument. if app_name is None: app_name = arg_app_name response, content = client.post( 'app/create/', { 'name': app_name or "<random>", }, ) if response.status == 201: print "Created http://%s.ep.io" % content['app']['names'][0] # Try to add a skeleton epio.ini file if not os.path.exists("epio.ini"): fh = open("epio.ini", "w") fh.write(open(os.path.join(os.path.dirname(__file__), "..", "skeleton", "epio.ini")).read()) fh.close() print "We've added a skeleton epio.ini file; please edit it to suit your app." else: print "You already seem to have an epio.ini file, so we're not going to add one." try: fh = open(".epio-app", "w") fh.write(content['app']['names'][0]) fh.close() except IOError: print "Could not write .epio-app file. You'll have to use -a on commands." elif response.status == 200: if "name" in content['form']['errors']: raise CommandError(", ".join(content['form']['errors']['name'])) else: raise CommandError("Unknown error: %s: %s" % (response.status, content)) elif response.status == 409: raise CommandError("An app called '%s' already exists. Try another name" % app_name) else: raise CommandError("Unknown error, %s: %s" % (response.status, content))
def handle_app(self, app, **options): client = self.get_client(options) response, content = client.post( 'app/%s/suspend/' % app, {}, ) if response.status in (200, 204): print "Suspended http://%s.ep.io" % self.app_name else: raise CommandError("Unknown error, %s: %s" % (response.status, content))
def make_key(self): "Creates a new SSH key" returncode = subprocess.call( [ "ssh-keygen", "-t", "rsa", "-f", SSH_IDENT, '-C', getpass.getuser() + "@" + platform.node() ], stdout=subprocess.PIPE, ) if returncode: raise CommandError( "Cannot create new SSH key; do you have the SSH client installed?" )
def handle_app_name(self, app, **options): # Make sure they have git try: subprocess.call(["git"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) except OSError: raise CommandError( "You must install git before you can use epio upload.") print "Uploading %s as app %s" % (os.path.abspath("."), app) # Make a temporary git repo, commit the current directory to it, and push repo_dir = tempfile.mkdtemp(prefix="epio-upload-") if 'Windows' in platform.platform() and not os.environ.has_key('HOME'): os.environ['HOME'] = os.environ['USERPROFILE'] #failsafe HOME try: # Copy the files across subprocess.Popen( ["cp", "-R", ".", repo_dir], stdout=subprocess.PIPE, cwd=os.getcwd(), ).communicate() # Remove any old git repo env = dict(os.environ) subprocess.Popen( ["rm", "-rf", ".git"], env=env, stdout=subprocess.PIPE, cwd=repo_dir, ).communicate() # Init the git repo subprocess.Popen( ["git", "init"], env=env, stdout=subprocess.PIPE, cwd=repo_dir, ).communicate() # Create a local ignore file fh = open(os.path.join(repo_dir, ".git/info/exclude"), "w") fh.write(".git\n.hg\n.svn\n.epio-git\n") if os.path.isfile(".epioignore"): fh2 = open(".epioignore") fh.write(fh2.read()) fh2.close() fh.close() # Remove any gitignore file subprocess.Popen( ["rm", "-f", ".gitignore"], env=env, stdout=subprocess.PIPE, cwd=repo_dir, ).communicate() # Set configuration options fh = open(os.path.join(repo_dir, ".git/config"), "w") fh.write("[core]\nautocrlf = false\n") fh.close() # Add files into git subprocess.Popen( ["git", "add", "."], env=env, stdout=subprocess.PIPE, cwd=repo_dir, ).communicate() # Commit them all subprocess.Popen( ["git", "commit", "-a", "-m", "Auto-commit."], env=env, stdout=subprocess.PIPE, cwd=repo_dir, ).communicate() # Push it subprocess.Popen( [ "git", "push", "-q", "vcs@%s:%s" % ( os.environ.get( 'EPIO_UPLOAD_HOST', os.environ.get('EPIO_HOST', 'upload.ep.io')).split(":")[0], app, ), "master" ], env=env, cwd=repo_dir, ).communicate() finally: # Remove the repo subprocess.call(["rm", "-rf", repo_dir])