def run(self): data = self.request("avail.datacenters") column_format = "{:<4} {:}" linodeutils.log_success(column_format.format(*("ID", "LOCATION"))) for data_center in data: row = (data_center.get("DATACENTERID"), data_center.get("LOCATION")) linodeutils.log_minor_success(column_format.format(*row))
def apply_rules(self, host_ip, rules, private_key): # connect to the server via ssh ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host_ip, username='******', allow_agent=False, look_for_keys=False, pkey=private_key) # copy the rules to the temp directory temp_file = '/tmp/' + str(uuid.uuid4()) ssh.open_sftp() sftp = ssh.open_sftp() sftp.open(temp_file, 'w').write(rules) # move the rules in to place and enable and run the iptables-restore.service commands = [ 'sudo mv ' + temp_file + ' /var/lib/iptables/rules-save', 'sudo chown root:root /var/lib/iptables/rules-save', 'sudo systemctl enable iptables-restore.service', 'sudo systemctl start iptables-restore.service' ] for command in commands: stdin, stdout, stderr = ssh.exec_command(command) stdout.channel.recv_exit_status() ssh.close() linodeutils.log_success('Applied rule to ' + host_ip)
def run(self): data = self.request('avail.datacenters') column_format = "{:<4} {:}" linodeutils.log_success(column_format.format(*('ID', 'LOCATION'))) for data_center in data: row = (data_center.get('DATACENTERID'), data_center.get('LOCATION')) linodeutils.log_minor_success(column_format.format(*row))
def run(self): data = self.request('avail.linodeplans') column_format = "{:<4} {:<16} {:<8} {:<12} {:}" linodeutils.log_success( column_format.format(*('ID', 'LABEL', 'CORES', 'RAM', 'PRICE'))) for plan in data: row = (plan.get('PLANID'), plan.get('LABEL'), plan.get('CORES'), str(plan.get('RAM')) + 'MB', '$' + str(plan.get('PRICE'))) linodeutils.log_minor_success(column_format.format(*row))
def run(self): data = self.request('avail.datacenters') column_format = "{:<4} {:}" linodeutils.log_success(column_format.format(*('ID', 'LOCATION'))) for data_center in data: row = ( data_center.get('DATACENTERID'), data_center.get('LOCATION') ) linodeutils.log_minor_success(column_format.format(*row))
def run(self): data = self.request('avail.linodeplans') column_format = "{:<4} {:<16} {:<8} {:<12} {:}" linodeutils.log_success(column_format.format(*('ID', 'LABEL', 'CORES', 'RAM', 'PRICE'))) for plan in data: row = ( plan.get('PLANID'), plan.get('LABEL'), plan.get('CORES'), str(plan.get('RAM')) + 'MB', '$' + str(plan.get('PRICE')) ) linodeutils.log_minor_success(column_format.format(*row))
def run(self): data = self.request("avail.linodeplans") column_format = "{:<4} {:<16} {:<8} {:<12} {:}" linodeutils.log_success(column_format.format(*("ID", "LABEL", "CORES", "RAM", "PRICE"))) for plan in data: row = ( plan.get("PLANID"), plan.get("LABEL"), plan.get("CORES"), str(plan.get("RAM")) + "MB", "$" + str(plan.get("PRICE")), ) linodeutils.log_minor_success(column_format.format(*row))
def main(): linodeutils.init() parser = argparse.ArgumentParser(description="Create Linode User Data") parser.add_argument( "--public-key", action="append", required=True, type=file, dest="public_key_files", help="Authorized SSH Keys" ) parser.add_argument("--etcd-token", required=False, default=None, dest="etcd_token", help="Etcd Token") args = parser.parse_args() etcd_token = args.etcd_token if etcd_token is None: etcd_token = generate_etcd_token() else: if not validate_etcd_token(args.etcd_token): raise ValueError("Invalid Etcd Token. You can generate a new token at https://discovery.etcd.io/new.") public_keys = [] for public_key_file in args.public_key_files: public_key = public_key_file.read() if validate_public_key(public_key): public_keys.append(public_key) else: linodeutils.log_warning("Invalid public key: " + public_key_file.name) if not len(public_keys) > 0: raise ValueError("Must supply at least one valid public key") linode_user_data = linodeutils.get_file("linode-user-data.yaml", "w", True) linode_template = linodeutils.get_file("linode-user-data-template.yaml") coreos_template = linodeutils.get_file("../coreos/user-data.example") coreos_template_string = coreos_template.read() coreos_template_string = coreos_template_string.replace( "#DISCOVERY_URL", "https://discovery.etcd.io/" + str(etcd_token) ) configuration_linode_template = yaml.safe_load(linode_template) configuration_coreos_template = yaml.safe_load(coreos_template_string) configuration = linodeutils.combine_dicts(configuration_coreos_template, configuration_linode_template) configuration["ssh_authorized_keys"] = public_keys dump = yaml.dump(configuration, default_flow_style=False, default_style="|") with linode_user_data as outfile: outfile.write("#cloud-config\n\n" + dump) linodeutils.log_success("Wrote Linode user data to " + linode_user_data.name)
def run(self): #NOTE: defaults to using display group, then manual input (via nodes/hosts), then discovery_url if self.node_display_group: self.acquire_linode_ips() nodes = self.deis_privateips hosts = self.deis_publicips else: nodes = self.nodes if self.nodes is not None else self.get_nodes_from_args( ) hosts = self.hosts if self.hosts is not None else nodes node_ips = [] for ip in nodes: if self.validate_ip_address(ip): node_ips.append(ip) else: linodeutils.log_warning( 'Invalid IP will not be added to security group: ' + ip) if not len(node_ips) > 0: raise ValueError('No valid IP addresses in security group.') host_ips = [] for ip in hosts: if self.validate_ip_address(ip): host_ips.append(ip) else: linodeutils.log_warning('Host has invalid IP address: ' + ip) if not len(host_ips) > 0: raise ValueError('No valid host addresses.') linodeutils.log_info('Generating iptables rules...') rules = self.get_firewall_contents(node_ips) linodeutils.log_success('Generated rules:') linodeutils.log_debug(rules) linodeutils.log_info('Applying rules...') self.apply_rules_to_all(host_ips, rules) linodeutils.log_success('Done!')
def run(self): #NOTE: defaults to using display group, then manual input (via nodes/hosts), then discovery_url if self.node_display_group: self.acquire_linode_ips() nodes = self.deis_privateips hosts = self.deis_publicips else: nodes = self.nodes if self.nodes is not None else self.get_nodes_from_args() hosts = self.hosts if self.hosts is not None else nodes node_ips = [] for ip in nodes: if self.validate_ip_address(ip): node_ips.append(ip) else: linodeutils.log_warning('Invalid IP will not be added to security group: ' + ip) if not len(node_ips) > 0: raise ValueError('No valid IP addresses in security group.') host_ips = [] for ip in hosts: if self.validate_ip_address(ip): host_ips.append(ip) else: linodeutils.log_warning('Host has invalid IP address: ' + ip) if not len(host_ips) > 0: raise ValueError('No valid host addresses.') linodeutils.log_info('Generating iptables rules...') rules = self.get_firewall_contents(node_ips) linodeutils.log_success('Generated rules:') linodeutils.log_debug(rules) linodeutils.log_info('Applying rules...') self.apply_rules_to_all(host_ips, rules) linodeutils.log_success('Done!')
def generate_etcd_token(): linodeutils.log_info("Generating new Etcd token...") data = requests.get("https://discovery.etcd.io/new").text token = data.replace("https://discovery.etcd.io/", "") linodeutils.log_success("Generated new token: " + token) return token
def main(): linodeutils.init() parser = argparse.ArgumentParser(description='Create Linode User Data') parser.add_argument('--public-key', action='append', required=True, type=file, dest='public_key_files', help='Authorized SSH Keys') parser.add_argument('--etcd-token', required=False, default=None, dest='etcd_token', help='Etcd Token') args = parser.parse_args() etcd_token = args.etcd_token if etcd_token is None: etcd_token = generate_etcd_token() else: if not validate_etcd_token(args.etcd_token): raise ValueError( 'Invalid Etcd Token. You can generate a new token at https://discovery.etcd.io/new.' ) public_keys = [] for public_key_file in args.public_key_files: public_key = public_key_file.read() if validate_public_key(public_key): public_keys.append(public_key) else: linodeutils.log_warning('Invalid public key: ' + public_key_file.name) if not len(public_keys) > 0: raise ValueError('Must supply at least one valid public key') linode_user_data = linodeutils.get_file("linode-user-data.yaml", "w", True) linode_template = linodeutils.get_file("linode-user-data-template.yaml") coreos_template = linodeutils.get_file("../coreos/user-data.example") coreos_template_string = coreos_template.read() coreos_template_string = coreos_template_string.replace( '#DISCOVERY_URL', 'https://discovery.etcd.io/' + str(etcd_token)) configuration_linode_template = yaml.safe_load(linode_template) configuration_coreos_template = yaml.safe_load(coreos_template_string) configuration = linodeutils.combine_dicts(configuration_coreos_template, configuration_linode_template) configuration['ssh_authorized_keys'] = public_keys dump = yaml.dump(configuration, default_flow_style=False, default_style='|') with linode_user_data as outfile: outfile.write("#cloud-config\n\n" + dump) linodeutils.log_success('Wrote Linode user data to ' + linode_user_data.name)
def generate_etcd_token(): linodeutils.log_info('Generating new Etcd token...') data = requests.get('https://discovery.etcd.io/new').text token = data.replace('https://discovery.etcd.io/', '') linodeutils.log_success('Generated new token: ' + token) return token