def __record(self, mode, name=None, path=None, timeout=0, **kwargs): if mode not in [PiCam.PHOTOCMD, PiCam.VIDEOCMD]: raise Exception("unknown mode: '%s'" % mode) if not name: name = "img_%s.jpg" % random.randint(1, 1000) target = name if path: path = os.expandvars(os.expanduser(path)) target = os.path.join(path, name) parts = [mode, "-o %s -t %s" % (target, timeout)] indicator = "-" if self.shortargs else "--" for key, value in kwargs.items(): if isinstance(value, bool): if value: parts.append("%s%s" % (indicator, key)) else: parts.append("%s%s %s" % (indicator, key, value)) cmd = " ".join(parts) os.system(cmd)
def launch_instance(ami='ami-7341831a', instance_type='t1.micro', key_name='paws', key_extension='.pem', key_dir='~/.ssh', group_name='paws', ssh_port=22, cidr='0.0.0.0/0', tag='paws', user_data=None, cmd_shell=True, login_user='******', ssh_passwd=None): """ Launch an instance and wait for it to start running. Returns a tuple consisting of the Instance object and the CmdShell object, if request, or None. ami The ID of the Amazon Machine Image that this instance will be based on. Default is a 64-bit Amazon Linux EBS image. instance_type The type of the instance. key_name The name of the SSH Key used for logging into the instance. It will be created if it does not exist. key_extension The file extension for SSH private key files. key_dir The path to the directory containing SSH private keys. This is usually ~/.ssh. group_name The name of the security group used to control access to the instance. It will be created if it does not exist. ssh_port The port number you want to use for SSH access (default 22). cidr The CIDR block used to limit access to your instance. tag A name that will be used to tag the instance so we can easily find it later. user_data Data that will be passed to the newly started instance at launch and will be accessible via the metadata service running at http://169.254.169.254. cmd_shell If true, a boto CmdShell object will be created and returned. This allows programmatic SSH access to the new instance. login_user The user name used when SSH'ing into new instance. The default is 'ec2-user' ssh_passwd The password for your SSH key if it is encrypted with a passphrase. """ cmd = None # Create a connection to EC2 service. # You can pass credentials in to the connect_ec2 method explicitly # or you can use the default credentials in your ~/.boto config file # as we are doing here. ec2 = boto.connect_ec2() # Check to see if specified keypair already exists. # If we get an InvalidKeyPair.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Creating keypair: %s' % key_name # Create an SSH key to use when logging into instances. key = ec2.create_key_pair(key_name) # Make sure the specified key_dir actually exists. # If not, create it. key_dir = os.expanduser(key_dir) key_dir = os.expandvars(key_dir) if not os.path.isdir(key_dir): os.mkdir(key_dir, 0700) # AWS will store the public key but the private key is # generated and returned and needs to be stored locally. # The save method will also chmod the file to protect # your private key. key.save(key_dir) else: raise
def detect_credentials(config_name, extra_environ=None, filenames=None, aws_profile_name=None, default_value=Ellipsis): ''' detect_credentials(config_name) attempts to locate Amazon S3 Bucket credentials from the given configuration item config_name. The following optional arguments are accepted: * extra_environ (default: None) may specify a string or a tuple (key_name, secret_name) or a list of strings or tuples; strings are treated as an additional environment variable that should be checked for credentials while tuples are treated as paired varialbes: if both are defined, then they are checked as separate holders of a key/secret pair. Note that a list of strings is considered a pair of solo environment varialbes while a tuple of strings is considered a single (key_name, secret_name) pair. * filenames (default: None) may specify a list of filenames that are checked in order for credentials. * aws_profile_name (default: None) may specify a profile name that appears in the ~/.aws/credentials file that will be checked for aws_access_key_id and aws_secret_access_key values. The files ~/.amazon/credentials and ~/.credentials are also checked. Note that this may be a list of profiles to check. * default_value (default: Ellipsis) may specify a value to return when no credentials are found; if this value is None, then it is always returned; otherwise, the value is passed through to_credentials() and any errors are allowed to propagate out of detect_credentials(). If default_value is Ellipsis then an error is simply raised stating that no credentials could be found. The detect_credentials() function looks at the following locations in the following order, assuming that it has been provided with the relevant information: * first, if the Neuropythy configuration variable config_name is set via either the npythyrc file or the associated environment variable, then it is coerced into credentials; * next, if the environment contains both the variables key_name and secret_name (from the optional argument key_secret_environ), then these values are used; * next, if the filenames argument is given, then all files it refers to are checked for credentials; these files are expanded with both os.expanduser and os.expandvars. * finally, if no credentials were detected, an error is raised. ''' # Check the config first: if config_name is not None and config[config_name] is not None: return config[config_name] # Okay, not found there; check the key/secret environment variables if extra_environ is None: extra_environ = [] elif pimms.is_str(extra_environ): extra_environ = [extra_environ] elif pimms.is_vector(extra_environ): if pimms.is_vector(extra_environ, str): if len(extra_environ) == 2 and isinstance(extra_environ, _tuple_type): extra_environ = [extra_environ] elif not pimms.is_matrix(extra_environ, str): raise ValueError('extra_environ must be a string, tuple of strings, or list of these') for ee in extra_environ: if pimms.is_str(ee): if ee in os.environ: try: return to_credentials(q) except: pass elif pimms.is_vector(ee, str) and len(ee) == 2: if ee[0] in os.environ and ee[1] in os.environ: (k,s) = [os.environ[q] for q in ee] if len(k) > 0 and len(s) > 0: continue return (k,s) else: raise ValueError('cannot interpret extra_environ argument: %s' % ee) # Okay, next we check the filenames if filenames is None: filenames = [] elif pimms.is_str(filenames): filenames = [filenames] for flnm in filenames: flnm = os.expanduser(os.expandvars(flnm)) if os.path.isfile(flnm): try: return to_credentials(flnm) except: pass # okay... let's check the AWS credentials file, if it exists if pimms.is_str(aws_profile_name): aws_profile_name = [aws_profile_name] elif aws_profile_name is None or len(aws_profile_name) == 0: aws_profile_name = None elif not pimms.is_vector(aws_profile_name, str): raise ValueError('Invalid aws_profile_name value: %s' % aws_profile_name) if aws_profile_name is not None: try: cc = confparse.ConfigParser() cc.read([os.expanduser(os.path.join('~', '.aws', 'credentials')), os.expanduser(os.path.join('~', '.amazon', 'credentials')), os.expanduser(os.path.join('~', '.credentials'))]) for awsprof in aws_profile_names: try: aws_access_key_id = cc.get(awsprof, 'aws_access_key_id') aws_secret_access_key = cc.get(awsprof, 'aws_secret_access_key') return (aws_access_key_id, aws_secret_access_key) except: pass except: pass # no match! if default_value is None: return None elif default_value is Ellipsis: if config_name is None: raise ValueError('No valid credentials were detected') else: raise ValueError('No valid credentials (%s) were detected' % config_name) else: return to_credentials(default_value)
def __init__(self, path=None, shortargs=False): if path: path = os.expandvars(os.expanduser(path)) os.chdir(path) self.path = path self.shortargs = shortargs