idx
int64
0
251k
question
stringlengths
53
3.53k
target
stringlengths
5
1.23k
len_question
int64
20
893
len_target
int64
3
238
244,500
def chdir ( directory ) : cur = os . getcwd ( ) try : yield os . chdir ( directory ) finally : os . chdir ( cur )
Change the current working directory to a different directory for a code block and return the previous directory after the block exits . Useful to run commands from a specificed directory .
35
33
244,501
def chownr ( path , owner , group , follow_links = True , chowntopdir = False ) : uid = pwd . getpwnam ( owner ) . pw_uid gid = grp . getgrnam ( group ) . gr_gid if follow_links : chown = os . chown else : chown = os . lchown if chowntopdir : broken_symlink = os . path . lexists ( path ) and not os . path . exists ( pa...
Recursively change user and group ownership of files and directories in given path . Doesn t chown path itself by default only its children .
218
28
244,502
def lchownr ( path , owner , group ) : chownr ( path , owner , group , follow_links = False )
Recursively change user and group ownership of files and directories in a given path not following symbolic links . See the documentation for os . lchown for more information .
29
34
244,503
def owner ( path ) : stat = os . stat ( path ) username = pwd . getpwuid ( stat . st_uid ) [ 0 ] groupname = grp . getgrgid ( stat . st_gid ) [ 0 ] return username , groupname
Returns a tuple containing the username & groupname owning the path .
59
13
244,504
def get_total_ram ( ) : with open ( '/proc/meminfo' , 'r' ) as f : for line in f . readlines ( ) : if line : key , value , unit = line . split ( ) if key == 'MemTotal:' : assert unit == 'kB' , 'Unknown unit' return int ( value ) * 1024 # Classic, not KiB. raise NotImplementedError ( )
The total amount of system RAM in bytes .
91
9
244,505
def add_to_updatedb_prunepath ( path , updatedb_path = UPDATEDB_PATH ) : if not os . path . exists ( updatedb_path ) or os . path . isdir ( updatedb_path ) : # If the updatedb.conf file doesn't exist then don't attempt to update # the file as the package providing mlocate may not be installed on # the local system retu...
Adds the specified path to the mlocate s udpatedb . conf PRUNEPATH list .
156
21
244,506
def install_ca_cert ( ca_cert , name = None ) : if not ca_cert : return if not isinstance ( ca_cert , bytes ) : ca_cert = ca_cert . encode ( 'utf8' ) if not name : name = 'juju-{}' . format ( charm_name ( ) ) cert_file = '/usr/local/share/ca-certificates/{}.crt' . format ( name ) new_hash = hashlib . md5 ( ca_cert ) . ...
Install the given cert as a trusted CA .
194
9
244,507
def get_audits ( ) : audits = [ ] audits . append ( TemplatedFile ( '/etc/securetty' , SecureTTYContext ( ) , template_dir = TEMPLATES_DIR , mode = 0o0400 , user = 'root' , group = 'root' ) ) return audits
Get OS hardening Secure TTY audits .
67
9
244,508
def dict_keys_without_hyphens ( a_dict ) : return dict ( ( key . replace ( '-' , '_' ) , val ) for key , val in a_dict . items ( ) )
Return the a new dict with underscores instead of hyphens in keys .
46
14
244,509
def update_relations ( context , namespace_separator = ':' ) : # Add any relation data prefixed with the relation type. relation_type = charmhelpers . core . hookenv . relation_type ( ) relations = [ ] context [ 'current_relation' ] = { } if relation_type is not None : relation_data = charmhelpers . core . hookenv . re...
Update the context with the relation data .
468
8
244,510
def juju_state_to_yaml ( yaml_path , namespace_separator = ':' , allow_hyphens_in_keys = True , mode = None ) : config = charmhelpers . core . hookenv . config ( ) # Add the charm_dir which we will need to refer to charm # file resources etc. config [ 'charm_dir' ] = charm_dir config [ 'local_unit' ] = charmhelpers . c...
Update the juju config and state in a yaml file .
468
13
244,511
def get_audits ( ) : if subprocess . call ( [ 'which' , 'apache2' ] , stdout = subprocess . PIPE ) != 0 : log ( "Apache server does not appear to be installed on this node - " "skipping apache hardening" , level = INFO ) return [ ] context = ApacheConfContext ( ) settings = utils . get_settings ( 'apache' ) audits = [ ...
Get Apache hardening config audits .
430
7
244,512
def get_audits ( ) : audits = [ ] settings = utils . get_settings ( 'os' ) # Remove write permissions from $PATH folders for all regular users. # This prevents changing system-wide commands from normal users. path_folders = { '/usr/local/sbin' , '/usr/local/bin' , '/usr/sbin' , '/usr/bin' , '/bin' } extra_user_paths = ...
Get OS hardening access audits .
273
7
244,513
def harden ( overrides = None ) : if overrides is None : overrides = [ ] def _harden_inner1 ( f ) : # As this has to be py2.7 compat, we can't use nonlocal. Use a trick # to capture the dictionary that can then be updated. _logged = { 'done' : False } def _harden_inner2 ( * args , * * kwargs ) : # knock out hardening v...
Hardening decorator .
458
5
244,514
def parse_mappings ( mappings , key_rvalue = False ) : parsed = { } if mappings : mappings = mappings . split ( ) for m in mappings : p = m . partition ( ':' ) if key_rvalue : key_index = 2 val_index = 0 # if there is no rvalue skip to next if not p [ 1 ] : continue else : key_index = 0 val_index = 2 key = p [ key_inde...
By default mappings are lvalue keyed .
124
10
244,515
def parse_data_port_mappings ( mappings , default_bridge = 'br-data' ) : # NOTE(dosaboy): we use rvalue for key to allow multiple values to be # proposed for <port> since it may be a mac address which will differ # across units this allowing first-known-good to be chosen. _mappings = parse_mappings ( mappings , key_rva...
Parse data port mappings .
208
7
244,516
def parse_vlan_range_mappings ( mappings ) : _mappings = parse_mappings ( mappings ) if not _mappings : return { } mappings = { } for p , r in six . iteritems ( _mappings ) : mappings [ p ] = tuple ( r . split ( ':' ) ) return mappings
Parse vlan range mappings .
75
8
244,517
def extract_tarfile ( archive_name , destpath ) : archive = tarfile . open ( archive_name ) archive . extractall ( destpath )
Unpack a tar archive optionally compressed
33
7
244,518
def extract_zipfile ( archive_name , destpath ) : archive = zipfile . ZipFile ( archive_name ) archive . extractall ( destpath )
Unpack a zip file
34
5
244,519
def get_address_in_network ( network , fallback = None , fatal = False ) : if network is None : if fallback is not None : return fallback if fatal : no_ip_found_error_out ( network ) else : return None networks = network . split ( ) or [ network ] for network in networks : _validate_cidr ( network ) network = netaddr ....
Get an IPv4 or IPv6 address within the network from the host .
330
15
244,520
def is_ipv6 ( address ) : try : address = netaddr . IPAddress ( address ) except netaddr . AddrFormatError : # probably a hostname - so not an address at all! return False return address . version == 6
Determine whether provided address is IPv6 or not .
53
12
244,521
def is_address_in_network ( network , address ) : try : network = netaddr . IPNetwork ( network ) except ( netaddr . core . AddrFormatError , ValueError ) : raise ValueError ( "Network (%s) is not in CIDR presentation format" % network ) try : address = netaddr . IPAddress ( address ) except ( netaddr . core . AddrForm...
Determine whether the provided address is within a network range .
122
13
244,522
def _get_for_address ( address , key ) : address = netaddr . IPAddress ( address ) for iface in netifaces . interfaces ( ) : addresses = netifaces . ifaddresses ( iface ) if address . version == 4 and netifaces . AF_INET in addresses : addr = addresses [ netifaces . AF_INET ] [ 0 ] [ 'addr' ] netmask = addresses [ neti...
Retrieve an attribute of or the physical interface that the IP address provided could be bound to .
302
19
244,523
def resolve_network_cidr ( ip_address ) : netmask = get_netmask_for_address ( ip_address ) return str ( netaddr . IPNetwork ( "%s/%s" % ( ip_address , netmask ) ) . cidr )
Resolves the full address cidr of an ip_address based on configured network interfaces
60
18
244,524
def get_iface_addr ( iface = 'eth0' , inet_type = 'AF_INET' , inc_aliases = False , fatal = True , exc_list = None ) : # Extract nic if passed /dev/ethX if '/' in iface : iface = iface . split ( '/' ) [ - 1 ] if not exc_list : exc_list = [ ] try : inet_num = getattr ( netifaces , inet_type ) except AttributeError : rai...
Return the assigned IP address for a given interface if any .
386
12
244,525
def get_iface_from_addr ( addr ) : for iface in netifaces . interfaces ( ) : addresses = netifaces . ifaddresses ( iface ) for inet_type in addresses : for _addr in addresses [ inet_type ] : _addr = _addr [ 'addr' ] # link local ll_key = re . compile ( "(.+)%.*" ) raw = re . match ( ll_key , _addr ) if raw : _addr = ra...
Work out on which interface the provided address is configured .
173
11
244,526
def sniff_iface ( f ) : def iface_sniffer ( * args , * * kwargs ) : if not kwargs . get ( 'iface' , None ) : kwargs [ 'iface' ] = get_iface_from_addr ( unit_get ( 'private-address' ) ) return f ( * args , * * kwargs ) return iface_sniffer
Ensure decorated function is called with a value for iface .
92
13
244,527
def get_ipv6_addr ( iface = None , inc_aliases = False , fatal = True , exc_list = None , dynamic_only = True ) : addresses = get_iface_addr ( iface = iface , inet_type = 'AF_INET6' , inc_aliases = inc_aliases , fatal = fatal , exc_list = exc_list ) if addresses : global_addrs = [ ] for addr in addresses : key_scope_li...
Get assigned IPv6 address for a given interface .
436
10
244,528
def get_bridges ( vnic_dir = '/sys/devices/virtual/net' ) : b_regex = "%s/*/bridge" % vnic_dir return [ x . replace ( vnic_dir , '' ) . split ( '/' ) [ 1 ] for x in glob . glob ( b_regex ) ]
Return a list of bridges on the system .
73
9
244,529
def get_bridge_nics ( bridge , vnic_dir = '/sys/devices/virtual/net' ) : brif_regex = "%s/%s/brif/*" % ( vnic_dir , bridge ) return [ x . split ( '/' ) [ - 1 ] for x in glob . glob ( brif_regex ) ]
Return a list of nics comprising a given bridge on the system .
78
14
244,530
def is_ip ( address ) : try : # Test to see if already an IPv4/IPv6 address address = netaddr . IPAddress ( address ) return True except ( netaddr . AddrFormatError , ValueError ) : return False
Returns True if address is a valid IP address .
53
10
244,531
def get_host_ip ( hostname , fallback = None ) : if is_ip ( hostname ) : return hostname ip_addr = ns_query ( hostname ) if not ip_addr : try : ip_addr = socket . gethostbyname ( hostname ) except Exception : log ( "Failed to resolve hostname '%s'" % ( hostname ) , level = WARNING ) return fallback return ip_addr
Resolves the IP for a given hostname or returns the input if it is already an IP .
94
20
244,532
def get_hostname ( address , fqdn = True ) : if is_ip ( address ) : try : import dns . reversename except ImportError : if six . PY2 : apt_install ( "python-dnspython" , fatal = True ) else : apt_install ( "python3-dnspython" , fatal = True ) import dns . reversename rev = dns . reversename . from_address ( address ) r...
Resolves hostname for given IP or returns the input if it is already a hostname .
179
19
244,533
def get_relation_ip ( interface , cidr_network = None ) : # Select the interface address first # For possible use as a fallback bellow with get_address_in_network try : # Get the interface specific IP address = network_get_primary_address ( interface ) except NotImplementedError : # If network-get is not available addr...
Return this unit s IP for the given interface .
247
10
244,534
def ensure_compliance ( self ) : for p in self . paths : if os . path . exists ( p ) : if self . is_compliant ( p ) : continue log ( 'File %s is not in compliance.' % p , level = INFO ) else : if not self . always_comply : log ( "Non-existent path '%s' - skipping compliance check" % ( p ) , level = INFO ) continue if s...
Ensure that the all registered files comply to registered criteria .
129
12
244,535
def is_compliant ( self , path ) : stat = self . _get_stat ( path ) user = self . user group = self . group compliant = True if stat . st_uid != user . pw_uid or stat . st_gid != group . gr_gid : log ( 'File %s is not owned by %s:%s.' % ( path , user . pw_name , group . gr_name ) , level = INFO ) compliant = False # PO...
Checks if the path is in compliance .
233
9
244,536
def comply ( self , path ) : utils . ensure_permissions ( path , self . user . pw_name , self . group . gr_name , self . mode )
Issues a chown and chmod to the file paths specified .
39
14
244,537
def is_compliant ( self , path ) : if not os . path . isdir ( path ) : log ( 'Path specified %s is not a directory.' % path , level = ERROR ) raise ValueError ( "%s is not a directory." % path ) if not self . recursive : return super ( DirectoryPermissionAudit , self ) . is_compliant ( path ) compliant = True for root ...
Checks if the directory is compliant .
136
8
244,538
def is_compliant ( self , path ) : same_templates = self . templates_match ( path ) same_content = self . contents_match ( path ) same_permissions = self . permissions_match ( path ) if same_content and same_permissions and same_templates : return True return False
Determines if the templated file is compliant .
68
12
244,539
def run_service_actions ( self ) : if not self . service_actions : return for svc_action in self . service_actions : name = svc_action [ 'service' ] actions = svc_action [ 'actions' ] log ( "Running service '%s' actions '%s'" % ( name , actions ) , level = DEBUG ) for action in actions : cmd = [ 'service' , name , acti...
Run any actions on services requested .
141
7
244,540
def comply ( self , path ) : dirname = os . path . dirname ( path ) if not os . path . exists ( dirname ) : os . makedirs ( dirname ) self . pre_write ( ) render_and_write ( self . template_dir , path , self . context ( ) ) utils . ensure_permissions ( path , self . user , self . group , self . mode ) self . run_servic...
Ensures the contents and the permissions of the file .
115
12
244,541
def templates_match ( self , path ) : template_path = get_template_path ( self . template_dir , path ) key = 'hardening:template:%s' % template_path template_checksum = file_hash ( template_path ) kv = unitdata . kv ( ) stored_tmplt_checksum = kv . get ( key ) if not stored_tmplt_checksum : kv . set ( key , template_ch...
Determines if the template files are the same .
241
11
244,542
def contents_match ( self , path ) : checksum = file_hash ( path ) kv = unitdata . kv ( ) stored_checksum = kv . get ( 'hardening:%s' % path ) if not stored_checksum : # If the checksum hasn't been generated, return False to ensure # the file is written and the checksum stored. log ( 'Checksum for %s has not been calcu...
Determines if the file content is the same .
136
11
244,543
def permissions_match ( self , path ) : audit = FilePermissionAudit ( path , self . user , self . group , self . mode ) return audit . is_compliant ( path )
Determines if the file owner and permissions match .
42
11
244,544
def save_checksum ( self , path ) : checksum = file_hash ( path ) kv = unitdata . kv ( ) kv . set ( 'hardening:%s' % path , checksum ) kv . flush ( )
Calculates and saves the checksum for the path specified .
54
13
244,545
def bool_from_string ( value ) : if isinstance ( value , six . string_types ) : value = six . text_type ( value ) else : msg = "Unable to interpret non-string value '%s' as boolean" % ( value ) raise ValueError ( msg ) value = value . strip ( ) . lower ( ) if value in [ 'y' , 'yes' , 'true' , 't' , 'on' ] : return True...
Interpret string value as boolean .
155
7
244,546
def bytes_from_string ( value ) : BYTE_POWER = { 'K' : 1 , 'KB' : 1 , 'M' : 2 , 'MB' : 2 , 'G' : 3 , 'GB' : 3 , 'T' : 4 , 'TB' : 4 , 'P' : 5 , 'PB' : 5 , } if isinstance ( value , six . string_types ) : value = six . text_type ( value ) else : msg = "Unable to interpret non-string value '%s' as bytes" % ( value ) raise...
Interpret human readable string value as bytes .
239
9
244,547
def audit ( * args ) : def wrapper ( f ) : test_name = f . __name__ if _audits . get ( test_name ) : raise RuntimeError ( "Test name '{}' used more than once" . format ( test_name ) ) non_callables = [ fn for fn in args if not callable ( fn ) ] if non_callables : raise RuntimeError ( "Configuration includes non-callabl...
Decorator to register an audit .
130
8
244,548
def is_audit_type ( * args ) : def _is_audit_type ( audit_options ) : if audit_options . get ( 'audit_type' ) in args : return True else : return False return _is_audit_type
This audit is included in the specified kinds of audits .
57
11
244,549
def run ( audit_options ) : errors = { } results = { } for name , audit in sorted ( _audits . items ( ) ) : result_name = name . replace ( '_' , '-' ) if result_name in audit_options . get ( 'excludes' , [ ] ) : print ( "Skipping {} because it is" "excluded in audit config" . format ( result_name ) ) continue if all ( ...
Run the configured audits with the specified audit_options .
294
11
244,550
def action_parse_results ( result ) : passed = True for test , result in result . items ( ) : if result [ 'success' ] : hookenv . action_set ( { test : 'PASS' } ) else : hookenv . action_set ( { test : 'FAIL - {}' . format ( result [ 'message' ] ) } ) passed = False if not passed : hookenv . action_fail ( "One or more ...
Parse the result of run in the context of an action .
105
13
244,551
def generate_selfsigned ( keyfile , certfile , keysize = "1024" , config = None , subject = None , cn = None ) : cmd = [ ] if config : cmd = [ "/usr/bin/openssl" , "req" , "-new" , "-newkey" , "rsa:{}" . format ( keysize ) , "-days" , "365" , "-nodes" , "-x509" , "-keyout" , keyfile , "-out" , certfile , "-config" , co...
Generate selfsigned SSL keypair
628
7
244,552
def ssh_directory_for_unit ( application_name , user = None ) : if user : application_name = "{}_{}" . format ( application_name , user ) _dir = os . path . join ( NOVA_SSH_DIR , application_name ) for d in [ NOVA_SSH_DIR , _dir ] : if not os . path . isdir ( d ) : os . mkdir ( d ) for f in [ 'authorized_keys' , 'known...
Return the directory used to store ssh assets for the application .
152
12
244,553
def ssh_known_host_key ( host , application_name , user = None ) : cmd = [ 'ssh-keygen' , '-f' , known_hosts ( application_name , user ) , '-H' , '-F' , host ] try : # The first line of output is like '# Host xx found: line 1 type RSA', # which should be excluded. output = subprocess . check_output ( cmd ) except subpr...
Return the first entry in known_hosts for host .
192
12
244,554
def remove_known_host ( host , application_name , user = None ) : log ( 'Removing SSH known host entry for compute host at %s' % host ) cmd = [ 'ssh-keygen' , '-f' , known_hosts ( application_name , user ) , '-R' , host ] subprocess . check_call ( cmd )
Remove the entry in known_hosts for host .
80
11
244,555
def is_same_key ( key_1 , key_2 ) : # The key format get will be like '|1|2rUumCavEXWVaVyB5uMl6m85pZo=|Cp' # 'EL6l7VTY37T/fg/ihhNb/GPgs= ssh-rsa AAAAB', we only need to compare # the part start with 'ssh-rsa' followed with '= ', because the hash # value in the beginning will change each time. k_1 = key_1 . split ( '= '...
Extract the key from two host entries and compare them .
159
12
244,556
def add_known_host ( host , application_name , user = None ) : cmd = [ 'ssh-keyscan' , '-H' , '-t' , 'rsa' , host ] try : remote_key = subprocess . check_output ( cmd ) . strip ( ) except Exception as e : log ( 'Could not obtain SSH host key from %s' % host , level = ERROR ) raise e current_key = ssh_known_host_key ( h...
Add the given host key to the known hosts file .
229
11
244,557
def ssh_authorized_key_exists ( public_key , application_name , user = None ) : with open ( authorized_keys ( application_name , user ) ) as keys : return ( '%s' % public_key ) in keys . read ( )
Check if given key is in the authorized_key file .
57
12
244,558
def add_authorized_key ( public_key , application_name , user = None ) : with open ( authorized_keys ( application_name , user ) , 'a' ) as keys : keys . write ( "{}\n" . format ( public_key ) )
Add given key to the authorized_key file .
57
10
244,559
def ssh_known_hosts_lines ( application_name , user = None ) : known_hosts_list = [ ] with open ( known_hosts ( application_name , user ) ) as hosts : for hosts_line in hosts : if hosts_line . rstrip ( ) : known_hosts_list . append ( hosts_line . rstrip ( ) ) return ( known_hosts_list )
Return contents of known_hosts file for given application .
90
12
244,560
def ssh_authorized_keys_lines ( application_name , user = None ) : authorized_keys_list = [ ] with open ( authorized_keys ( application_name , user ) ) as keys : for authkey_line in keys : if authkey_line . rstrip ( ) : authorized_keys_list . append ( authkey_line . rstrip ( ) ) return ( authorized_keys_list )
Return contents of authorized_keys file for given application .
88
11
244,561
def ssh_compute_remove ( public_key , application_name , user = None ) : if not ( os . path . isfile ( authorized_keys ( application_name , user ) ) or os . path . isfile ( known_hosts ( application_name , user ) ) ) : return keys = ssh_authorized_keys_lines ( application_name , user = None ) keys = [ k . strip ( ) for...
Remove given public key from authorized_keys file .
183
10
244,562
def apt_cache ( in_memory = True , progress = None ) : from apt import apt_pkg apt_pkg . init ( ) if in_memory : apt_pkg . config . set ( "Dir::Cache::pkgcache" , "" ) apt_pkg . config . set ( "Dir::Cache::srcpkgcache" , "" ) return apt_pkg . Cache ( progress )
Build and return an apt cache .
82
7
244,563
def apt_mark ( packages , mark , fatal = False ) : log ( "Marking {} as {}" . format ( packages , mark ) ) cmd = [ 'apt-mark' , mark ] if isinstance ( packages , six . string_types ) : cmd . append ( packages ) else : cmd . extend ( packages ) if fatal : subprocess . check_call ( cmd , universal_newlines = True ) else ...
Flag one or more packages using apt - mark .
104
10
244,564
def import_key ( key ) : key = key . strip ( ) if '-' in key or '\n' in key : # Send everything not obviously a keyid to GPG to import, as # we trust its validation better than our own. eg. handling # comments before the key. log ( "PGP key found (looks like ASCII Armor format)" , level = DEBUG ) if ( '-----BEGIN PGP P...
Import an ASCII Armor key .
460
6
244,565
def _dearmor_gpg_key ( key_asc ) : ps = subprocess . Popen ( [ 'gpg' , '--dearmor' ] , stdout = subprocess . PIPE , stderr = subprocess . PIPE , stdin = subprocess . PIPE ) out , err = ps . communicate ( input = key_asc ) # no need to decode output as it is binary (invalid utf-8), only error if six . PY3 : err = err . ...
Converts a GPG key in the ASCII armor format to the binary format .
186
16
244,566
def _write_apt_gpg_keyfile ( key_name , key_material ) : with open ( '/etc/apt/trusted.gpg.d/{}.gpg' . format ( key_name ) , 'wb' ) as keyf : keyf . write ( key_material )
Writes GPG key material into a file at a provided path .
67
14
244,567
def _add_apt_repository ( spec ) : if '{series}' in spec : series = get_distrib_codename ( ) spec = spec . replace ( '{series}' , series ) # software-properties package for bionic properly reacts to proxy settings # passed as environment variables (See lp:1433761). This is not the case # LTS and non-LTS releases below ...
Add the spec using add_apt_repository
137
11
244,568
def _add_cloud_distro_check ( cloud_archive_release , openstack_release ) : _verify_is_ubuntu_rel ( cloud_archive_release , openstack_release ) _add_cloud_pocket ( "{}-{}" . format ( cloud_archive_release , openstack_release ) )
Add the cloud pocket but also check the cloud_archive_release against the current distro and use the openstack_release as the full lookup .
71
30
244,569
def _verify_is_ubuntu_rel ( release , os_release ) : ubuntu_rel = get_distrib_codename ( ) if release != ubuntu_rel : raise SourceConfigError ( 'Invalid Cloud Archive release specified: {}-{} on this Ubuntu' 'version ({})' . format ( release , os_release , ubuntu_rel ) )
Verify that the release is in the same as the current ubuntu release .
80
16
244,570
def _run_with_retries ( cmd , max_retries = CMD_RETRY_COUNT , retry_exitcodes = ( 1 , ) , retry_message = "" , cmd_env = None ) : env = None kwargs = { } if cmd_env : env = os . environ . copy ( ) env . update ( cmd_env ) kwargs [ 'env' ] = env if not retry_message : retry_message = "Failed executing '{}'" . format ( "...
Run a command and retry until success or max_retries is reached .
276
16
244,571
def _run_apt_command ( cmd , fatal = False ) : # Provide DEBIAN_FRONTEND=noninteractive if not present in the environment. cmd_env = { 'DEBIAN_FRONTEND' : os . environ . get ( 'DEBIAN_FRONTEND' , 'noninteractive' ) } if fatal : _run_with_retries ( cmd , cmd_env = cmd_env , retry_exitcodes = ( 1 , APT_NO_LOCK , ) , retr...
Run an apt command with optional retries .
160
9
244,572
def get_upstream_version ( package ) : import apt_pkg cache = apt_cache ( ) try : pkg = cache [ package ] except Exception : # the package is unknown to the current apt cache. return None if not pkg . current_ver : # package is known, but no version is currently installed. return None return apt_pkg . upstream_version ...
Determine upstream version based on installed package
90
9
244,573
def get_bcache_fs ( ) : cachesetroot = "{}/fs/bcache" . format ( SYSFS ) try : dirs = os . listdir ( cachesetroot ) except OSError : log ( "No bcache fs found" ) return [ ] cacheset = set ( [ Bcache ( '{}/{}' . format ( cachesetroot , d ) ) for d in dirs if not d . startswith ( 'register' ) ] ) return cacheset
Return all cache sets
110
4
244,574
def get_stats_action ( cachespec , interval ) : if cachespec == 'global' : caches = get_bcache_fs ( ) else : caches = [ Bcache . fromdevice ( cachespec ) ] res = dict ( ( c . cachepath , c . get_stats ( interval ) ) for c in caches ) return json . dumps ( res , indent = 4 , separators = ( ',' , ': ' ) )
Action for getting bcache statistics for a given cachespec . Cachespec can either be a device name eg . sdb which will retrieve cache stats for the given device or global which will retrieve stats for all cachesets
93
44
244,575
def get_stats ( self , interval ) : intervaldir = 'stats_{}' . format ( interval ) path = "{}/{}" . format ( self . cachepath , intervaldir ) out = dict ( ) for elem in os . listdir ( path ) : out [ elem ] = open ( '{}/{}' . format ( path , elem ) ) . read ( ) . strip ( ) return out
Get cache stats
96
3
244,576
def update_dns_ha_resource_params ( resources , resource_params , relation_id = None , crm_ocf = 'ocf:maas:dns' ) : _relation_data = { 'resources' : { } , 'resource_params' : { } } update_hacluster_dns_ha ( charm_name ( ) , _relation_data , crm_ocf ) resources . update ( _relation_data [ 'resources' ] ) resource_params...
Configure DNS - HA resources based on provided configuration and update resource dictionaries for the HA relation .
148
20
244,577
def expect_ha ( ) : ha_related_units = [ ] try : ha_related_units = list ( expected_related_units ( reltype = 'ha' ) ) except ( NotImplementedError , KeyError ) : pass return len ( ha_related_units ) > 0 or config ( 'vip' ) or config ( 'dns-ha' )
Determine if the unit expects to be in HA
81
11
244,578
def generate_ha_relation_data ( service , extra_settings = None ) : _haproxy_res = 'res_{}_haproxy' . format ( service ) _relation_data = { 'resources' : { _haproxy_res : 'lsb:haproxy' , } , 'resource_params' : { _haproxy_res : 'op monitor interval="5s"' } , 'init_services' : { _haproxy_res : 'haproxy' } , 'clones' : {...
Generate relation data for ha relation
279
7
244,579
def update_hacluster_dns_ha ( service , relation_data , crm_ocf = 'ocf:maas:dns' ) : # Validate the charm environment for DNS HA assert_charm_supports_dns_ha ( ) settings = [ 'os-admin-hostname' , 'os-internal-hostname' , 'os-public-hostname' , 'os-access-hostname' ] # Check which DNS settings are set and update dictio...
Configure DNS - HA resources based on provided configuration
560
10
244,580
def get_vip_settings ( vip ) : iface = get_iface_for_address ( vip ) netmask = get_netmask_for_address ( vip ) fallback = False if iface is None : iface = config ( 'vip_iface' ) fallback = True if netmask is None : netmask = config ( 'vip_cidr' ) fallback = True return iface , netmask , fallback
Calculate which nic is on the correct network for the given vip .
102
16
244,581
def update_hacluster_vip ( service , relation_data ) : cluster_config = get_hacluster_config ( ) vip_group = [ ] vips_to_delete = [ ] for vip in cluster_config [ 'vip' ] . split ( ) : if is_ipv6 ( vip ) : res_vip = 'ocf:heartbeat:IPv6addr' vip_params = 'ipv6addr' else : res_vip = 'ocf:heartbeat:IPaddr2' vip_params = 'i...
Configure VIP resources based on provided configuration
726
8
244,582
def _add_services ( self , this_service , other_services ) : if this_service [ 'name' ] != os . path . basename ( os . getcwd ( ) ) : s = this_service [ 'name' ] msg = "The charm's root directory name needs to be {}" . format ( s ) amulet . raise_status ( amulet . FAIL , msg = msg ) if 'units' not in this_service : thi...
Add services .
303
3
244,583
def _add_relations ( self , relations ) : for k , v in six . iteritems ( relations ) : self . d . relate ( k , v )
Add all of the relations for the services .
34
9
244,584
def _configure_services ( self , configs ) : for service , config in six . iteritems ( configs ) : self . d . configure ( service , config )
Configure all of the services .
37
7
244,585
def _deploy ( self ) : timeout = int ( os . environ . get ( 'AMULET_SETUP_TIMEOUT' , 900 ) ) try : self . d . setup ( timeout = timeout ) self . d . sentry . wait ( timeout = timeout ) except amulet . helpers . TimeoutError : amulet . raise_status ( amulet . FAIL , msg = "Deployment timed out ({}s)" . format ( timeout ...
Deploy environment and wait for all hooks to finish executing .
100
11
244,586
def _init_ca ( self ) : if not exists ( path_join ( self . ca_dir , 'ca.cnf' ) ) : with open ( path_join ( self . ca_dir , 'ca.cnf' ) , 'w' ) as fh : fh . write ( CA_CONF_TEMPLATE % ( self . get_conf_variables ( ) ) ) if not exists ( path_join ( self . ca_dir , 'signing.cnf' ) ) : with open ( path_join ( self . ca_dir ...
Generate the root ca s cert and key .
337
10
244,587
def format_endpoint ( schema , addr , port , api_version ) : return '{}://{}:{}/{}/' . format ( schema , addr , port , get_api_suffix ( api_version ) )
Return a formatted keystone endpoint
52
6
244,588
def get_keystone_manager ( endpoint , api_version , * * kwargs ) : if api_version == 2 : return KeystoneManager2 ( endpoint , * * kwargs ) if api_version == 3 : return KeystoneManager3 ( endpoint , * * kwargs ) raise ValueError ( 'No manager found for api version {}' . format ( api_version ) )
Return a keystonemanager for the correct API version
82
11
244,589
def get_keystone_manager_from_identity_service_context ( ) : context = IdentityServiceContext ( ) ( ) if not context : msg = "Identity service context cannot be generated" log ( msg , level = ERROR ) raise ValueError ( msg ) endpoint = format_endpoint ( context [ 'service_protocol' ] , context [ 'service_host' ] , cont...
Return a keystonmanager generated from a instance of charmhelpers . contrib . openstack . context . IdentityServiceContext
185
25
244,590
def resolve_service_id ( self , service_name = None , service_type = None ) : services = [ s . _info for s in self . api . services . list ( ) ] service_name = service_name . lower ( ) for s in services : name = s [ 'name' ] . lower ( ) if service_type and service_name : if ( service_name == name and service_type == s ...
Find the service_id of a given service
151
9
244,591
def deactivate_lvm_volume_group ( block_device ) : vg = list_lvm_volume_group ( block_device ) if vg : cmd = [ 'vgchange' , '-an' , vg ] check_call ( cmd )
Deactivate any volume gruop associated with an LVM physical volume .
58
14
244,592
def remove_lvm_physical_volume ( block_device ) : p = Popen ( [ 'pvremove' , '-ff' , block_device ] , stdin = PIPE ) p . communicate ( input = 'y\n' )
Remove LVM PV signatures from a given block device .
56
11
244,593
def list_lvm_volume_group ( block_device ) : vg = None pvd = check_output ( [ 'pvdisplay' , block_device ] ) . splitlines ( ) for lvm in pvd : lvm = lvm . decode ( 'UTF-8' ) if lvm . strip ( ) . startswith ( 'VG Name' ) : vg = ' ' . join ( lvm . strip ( ) . split ( ) [ 2 : ] ) return vg
List LVM volume group associated with a given block device .
108
12
244,594
def list_logical_volumes ( select_criteria = None , path_mode = False ) : lv_diplay_attr = 'lv_name' if path_mode : # Parsing output logic relies on the column order lv_diplay_attr = 'vg_name,' + lv_diplay_attr cmd = [ 'lvs' , '--options' , lv_diplay_attr , '--noheadings' ] if select_criteria : cmd . extend ( [ '--sele...
List logical volumes
202
3
244,595
def create_logical_volume ( lv_name , volume_group , size = None ) : if size : check_call ( [ 'lvcreate' , '--yes' , '-L' , '{}' . format ( size ) , '-n' , lv_name , volume_group ] ) # create the lv with all the space available, this is needed because the # system call is different for LVM else : check_call ( [ 'lvcrea...
Create a new logical volume in an existing volume group
137
10
244,596
def render ( source , target , context , owner = 'root' , group = 'root' , perms = 0o444 , templates_dir = None , encoding = 'UTF-8' , template_loader = None , config_template = None ) : try : from jinja2 import FileSystemLoader , Environment , exceptions except ImportError : try : from charmhelpers . fetch import apt_...
Render a template .
459
4
244,597
def cached ( func ) : @ wraps ( func ) def wrapper ( * args , * * kwargs ) : global cache key = json . dumps ( ( func , args , kwargs ) , sort_keys = True , default = str ) try : return cache [ key ] except KeyError : pass # Drop out of the exception handler scope. res = func ( * args , * * kwargs ) cache [ key ] = res...
Cache return values for multiple executions of func + args
103
10
244,598
def flush ( key ) : flush_list = [ ] for item in cache : if key in item : flush_list . append ( item ) for item in flush_list : del cache [ item ]
Flushes any entries from function cache where the key is found in the function + args
42
17
244,599
def log ( message , level = None ) : command = [ 'juju-log' ] if level : command += [ '-l' , level ] if not isinstance ( message , six . string_types ) : message = repr ( message ) command += [ message [ : SH_MAX_ARG ] ] # Missing juju-log should not cause failures in unit tests # Send log output to stderr try : subpro...
Write a message to the juju log
164
8