def complete(index): try: completed = self._todos[index - 1] completed.complete() clip.echo('Marked todo "{}" as completed'.format(completed._item)) except IndexError: clip.exit('Invalid todo index given', True)
def status (): rc = 0 for module in sorted ([f.basename () for f in CONFIG.module_dir.glob ("*")]): rc += implementation ("check", module = module) if rc: clip.exit (err = True)
def consume(self, tokens): tokens.pop(0) # Pop the choice from the tokens array selected = tokens.pop(0) if selected not in self._choices: clip.exit('Error: "{}" is not a valid choice (choose from {}).'.format(selected, ', '.join(self._choices)), True) clip.Parameter.post_consume(self, selected) return tokens
def do(**kwargs): load_config(kwargs["templates"]) f = kwargs.get("output") formats = (None, "JSON", "json", "YAML", "yaml", "yml", "TOML", "toml") if f not in formats: clip.exit(err=True, message="Unknown format (%s), not one of: %s" % (f, formats)) print Config.dumps(form=f)
def amqp_delete_user(user): users = USER_MAP.get('users', {}) if user not in users: print_failure('No such user \'%s\' configured' % user) clip.exit(err=True) del users[user] USER_MAP.write() print_success("Configuration for user '%s' deleted" % user)
def upload(zipfile, params): client = boto3.client('lambda') with open(zipfile + ".zip", 'rb') as f: params['FunctionZip'] = f try: response = client.upload_function(**params) except Exception as e: clip.exit(e, err=True)
def run (self): if not self.conf.force: self.error (" Must force writes (--force).") clip.exit (err = True) rc = self.process_cfgs ("Generate...", self.conf.templ_ext, "", self.make_file) clip.exit (err = (True if rc else False))
def complete(index): try: completed = self._todos[index - 1] completed.complete() clip.echo('Marked todo "{}" as completed'.format( completed._item)) except IndexError: clip.exit('Invalid todo index given', True)
def _parse_url (self, typ, url): _urlspec = namedtuple ("_UrlSpec", ["protocol", "bucket", "key"]) p = urlparse.urlparse (url) rc = _urlspec (protocol = p.scheme, bucket = p.netloc, key = p.path[1:] if p.path.startswith ("/") else p.path) if rc.protocol != "s3": self.error ("%s protocol is not s3: %s" % (typ, url)) clip.exit (err = True) return rc
def consume(self, tokens): tokens.pop(0) # Pop the choice from the tokens array selected = tokens.pop(0) if selected not in self._choices: clip.exit( 'Error: "{}" is not a valid choice (choose from {}).'. format(selected, ', '.join(self._choices)), True) clip.Parameter.post_consume(self, selected) return tokens
def deploy_file(path, kwargs, config): with utils.directory(os.path.dirname(path)): config.update(kwargs) if 'FunctionName' not in config: clip.exit('You must provide a function name', err=True) # Zip up directory utils.make_zip(config['FunctionName']) # Upload! upload(config['FunctionName'], config)
def belief (**kwargs): rc = 0 if kwargs.get ("module"): rc += implementation ("belief", **kwargs) else: for module in sorted ([f.basename () for f in CONFIG.module_dir.glob ("*")]): rc += implementation ("belief", module = module) if rc: clip.exit (err = True)
def run (self): if self._check (): clip.exit (err = True) self._list_from () with tempfile.NamedTemporaryFile (prefix = "s3clumper_tar__") as out_f: self._entar (out_f) self._send (out_f) self._cleanup () if not self.args.quiet: print ""
def run (self): value = (self.belief if not self.kwargs.belief else self.get_belief (self.kwargs.belief)) if not value: self.error ("Belief not found: %s" % self.kwargs.belief) clip.exit (err = True) elif isinstance (value, numbers.Number) or isinstance (value, string_types): self.report (value) else: self.report (json.dumps (value, indent = 2)) return 0
def verify(self): if self.get_config() is None: clip.exit('Configuration has not been specified!', err=True) if 'FunctionName' not in self.get_config(): clip.exit('You must provide a function name', err=True) # Fill in from global config if self._global_config: role = self.get_config('Role', 'default') if not role.startswith('arn:aws:iam::'): roles = self._global_config.get('roles') if roles and role in roles: self._config['config']['Role'] = roles[role]
def test_exit(self): # Standard case, custom message try: clip.exit('Woot!') except clip.ClipExit as e: self.assertEqual(e.message, 'Woot!') # Error condition try: clip.exit(err=True) except clip.ClipExit as e: self.assertTrue(e.message.startswith('clip exiting')) self.assertEqual(e.status, 1)
def test_exit(self): self.embed() # Standard case, custom message try: clip.exit('Woot!') except clip.ClipExit as e: self.assertEqual(e.message, 'Woot!') # Error condition try: clip.exit(err=True) except clip.ClipExit as e: self.assertTrue(e.message.startswith('clip exiting')) self.assertEqual(e.status, 1)
def do(**kwargs): typ = kwargs["typ"] name = kwargs["name"] n = kwargs["n"] key = kwargs["key"] load_config(kwargs["templates"]) components = Config.get("DEFAULT/COMPONENTS") if typ not in components: clip.exit(err=True, message="Unknown object typ: %s" % typ) try: tl = tile.Tile(typ, None, name, n) print "%s => %s" % (key, tl.value(key)) except: # pylint: disable=bare-except clip.exit(err=True, message="Key not found. KeyError: %s" % key)
def amqp_send(host, port, exchange, routing_key, message, file_path, user, persistent, vhost, ssl, **kwargs): if not user: user = os.getenv('AMQP_USER', None) try: if not user: user = input(Fore.GREEN + "User: "******"\nTerminated from keyboard") sys.exit(1) if not ((message is None) ^ (file_path is None)): print_failure('Exactly one option (-f) or (-m) ' 'must be specified', ) clip.exit(err=True) properties = pika.BasicProperties(delivery_mode=2 if persistent else 1) credentials = pika.PlainCredentials(user, password) sys.stdout.write("Connecting to queue @ %s:%s... " % (host, port)) try: connection = pika.BlockingConnection( pika.ConnectionParameters(host=host, port=port, ssl=ssl, virtual_host=vhost, credentials=credentials)) except pika.exceptions.AMQPError as e: print_failure("FAILED!", out=sys.stdout) print_failure("Failure reason: %s" % repr(e)) sys.exit(1) print_success("SUCCESS!") if message: body = message.encode('utf-8') else: with open(file_path, 'rb') as f: body = f.read() channel = connection.channel() try: channel.basic_publish(exchange=exchange, routing_key=routing_key, body=body, properties=properties) finally: channel.close() print_success("Message successfully published to exchange [%s]!" % exchange)
def uri_type(uri): if os.path.isdir(uri): return 'directory' if os.path.isfile(uri): return 'file' if uri.startswith('s3:'): return 's3' g = GitURL(uri) if g.valid: return 'gist' if g.is_a('gist') else 'repo' try: requests.get(uri) return 'url' except: pass clip.exit('Unrecognized URI', err=True)
def amqp_send(host, port, exchange, routing_key, message, file_path, user, persistent, vhost, ssl, **kwargs): if not user: user = os.getenv('AMQP_USER', None) try: if not user: user = input(Fore.GREEN + "User: "******"\nTerminated from keyboard") sys.exit(1) if not ((message is None) ^ (file_path is None)): print_failure('Exactly one option (-f) or (-m) ' 'must be specified',) clip.exit(err=True) properties = pika.BasicProperties(delivery_mode=2 if persistent else 1) credentials = pika.PlainCredentials(user, password) sys.stdout.write("Connecting to queue @ %s:%s... " % (host, port)) try: connection = pika.BlockingConnection(pika.ConnectionParameters( host=host, port=port, ssl=ssl, virtual_host=vhost, credentials=credentials)) except pika.exceptions.AMQPError as e: print_failure("FAILED!", out=sys.stdout) print_failure("Failure reason: %s" % repr(e)) sys.exit(1) print_success("SUCCESS!") if message: body = message.encode('utf-8') else: with open(file_path, 'rb') as f: body = f.read() channel = connection.channel() try: channel.basic_publish(exchange=exchange, routing_key=routing_key, body=body, properties=properties) finally: channel.close() print_success("Message successfully published to exchange [%s]!" % exchange)
def deploy_dir(path, kwargs): with utils.directory(path): config = utils.load_config() config['config'].update(kwargs) if 'FunctionName' not in config['config']: clip.exit('You must provide a function name', err=True) # Remove ignore paths for e in config['ignore'] + ['.git/', '.gitignore']: utils.delete_resource(e) # Run install command if 'install' in config: utils.shell(config['install']) # Zip up directory utils.make_zip(config['config']['FunctionName']) # Upload! params = config['config'] upload(params['FunctionName'], params)
def upload(params): zipfile = params['FunctionName'] utils.make_zip(zipfile) # Zip up directory with open(zipfile + ".zip", 'rb') as f: zip_bytes = f.read() # Now we're ready to upload! clip.echo('Deploying function "{}"...'.format(zipfile)) client = boto3.client('lambda') upsert = params.pop('upsert', False) try: if upsert: # We can override an existing function, check if we need to try: client.get_function_configuration(FunctionName=zipfile) # If we made it here, we need to update response = client.update_function_code(FunctionName=zipfile, ZipFile=zip_bytes) clip.echo('Update code response: {}'.format( response['ResponseMetadata']['HTTPStatusCode'] )) params.pop('Runtime', None) response = client.update_function_configuration(**params) clip.echo('Update configuration response: {}'.format( response['ResponseMetadata']['HTTPStatusCode'] )) clip.echo('ARN: {}'.format(response['FunctionArn'])) return except Exception as e: if 'ResourceNotFoundException' not in str(e): raise e # Create the function from scratch params['Code'] = { 'ZipFile': zip_bytes } response = client.create_function(**params) clip.echo('Create response: {}'.format( response['ResponseMetadata']['HTTPStatusCode'] )) clip.echo('ARN: {}'.format(response['FunctionArn'])) except Exception as e: clip.exit(e, err=True)
def run (self): if not self.conf.force: self.error (" Must force writes (--force).") clip.exit (err = True) rc = False content = None in_file = Path (self.kwargs.in_file) if not in_file.isfile (): self.error ("Template file does not exist.") clip.exit (err = True) self.info (" Generate...") if self.kwargs.out_file == "=": f = Path (self.kwargs.in_file) out_file = f.parent / f.namebase rc = self.process_one_file (in_file, out_file, self.make_file) content = out_file.bytes () elif self.kwargs.out_file: out_file = Path (self.kwargs.out_file) rc = self.process_one_file (in_file, out_file, self.make_file) content = out_file.bytes () else: with temporary.temp_file () as fname: out_file = Path (fname) rc = self.process_one_file (in_file, out_file, self.make_file) content = out_file.bytes () if not self.kwargs.out_file: self.info ("Produced file:") self.report (content) clip.exit (err = (True if rc else False))
def upload(params): zipfile = params['FunctionName'] utils.make_zip(zipfile) # Zip up directory with open(zipfile + ".zip", 'rb') as f: zip_bytes = f.read() # Now we're ready to upload! clip.echo('Deploying function "{}"...'.format(zipfile)) client = boto3.client('lambda') upsert = params.pop('upsert', False) try: if upsert: # We can override an existing function, check if we need to try: client.get_function_configuration(FunctionName=zipfile) # If we made it here, we need to update response = client.update_function_code(FunctionName=zipfile, ZipFile=zip_bytes) clip.echo('Update code response: {}'.format( response['ResponseMetadata']['HTTPStatusCode'])) params.pop('Runtime', None) response = client.update_function_configuration(**params) clip.echo('Update configuration response: {}'.format( response['ResponseMetadata']['HTTPStatusCode'])) clip.echo('ARN: {}'.format(response['FunctionArn'])) return except Exception as e: if 'ResourceNotFoundException' not in str(e): raise e # Create the function from scratch params['Code'] = {'ZipFile': zip_bytes} response = client.create_function(**params) clip.echo('Create response: {}'.format( response['ResponseMetadata']['HTTPStatusCode'])) clip.echo('ARN: {}'.format(response['FunctionArn'])) except Exception as e: clip.exit(e, err=True)
def run (self): if not self.conf.force: self.error (" Must force installs (--force).") clip.exit (err = True) try: fpath = pkg_resources.resource_filename (self.kwargs.module, "_cfgtool/") fname = pkg_resources.resource_filename (self.kwargs.module, "_cfgtool/install") except OSError as e: self.error ("Module %s does not have a cfgtool install." % self.kwargs.module) clip.exit (err = True) try: self.debug (" Running: %s" % fname) subprocess.check_call ([fname,], cwd = fpath, shell = True, universal_newlines = True) except subprocess.CalledProcessError as e: self.error (" Error running installer: %d" % e.returncode) clip.exit (err = True)
def option_version (opt): # pylint: disable = W0613 print (__version__) clip.exit ()
def remove(index): try: removed = self._todos.pop(index - 1) clip.echo('Removed todo "{}"'.format(removed._item)) except IndexError: clip.exit('Invalid todo index given', True)
def check (module): rc = implementation ("check", module = module) if rc: clip.exit (err = True)
def run (self): rc = self.process_cfgs ("Generate...", self.conf.templ_ext, self.conf.sample_ext, self.make_file) clip.exit (err = (True if rc else False))
def test_exit_message(self): # Exiting should print a message _, out, _ = self.make_embedded_app() with self.assertRaises(clip.ClipExit): clip.exit('Exiting!') self.assertEqual(out._writes, ['Exiting!\n'])
def print_version(value): clip.exit('lfm version {}'.format(__version__))
def run(uri, dest): uri_type = utils.uri_type(uri) if uri_type in ['directory', 'file']: clip.exit('Cannot download local file, exiting...') return globals()['download_{}'.format(uri_type)](uri, dest)
def run (self): for module in self.cfgs: rc = Action (module).run () clip.exit (err = (True if rc else False))
def option_version(opt): # pylint: disable = W0613 print __version__ clip.exit()
def print_version(value): clip.exit('Version 0.0.0')
def exit_if_forced (self, msg = None): self.error (msg) if not self.conf.force: self.warn ("Force not passed. Exiting...") clip.exit (err = True) self.warn ("Force passed. Continueing after error...")