def set_hostname(): macaddress = NetInfo.get_default_mac().replace(":", "") default_hostname = "pi-" + macaddress init_hostname = socket.gethostname() logger.debug("get hostname = %s" % init_hostname) # self.hostname = inithostname if init_hostname != default_hostname: logger.debug("set hostname = %s" % init_hostname) # 重设主机名 args = "hostname %s " % default_hostname subprocess.Popen(args, shell=True, stdout=subprocess.PIPE).communicate() # 修改/etc/hostname文件 f = open('/etc/hostname', 'w') f.write(default_hostname) f.close() # 修改/etc/hosts if 'linux' in sys.platform or 'darwin' in sys.platform: filename = '/etc/hosts' else: filename = 'c:\windows\system32\drivers\etc\hosts' hosts = Hosts(path=filename) # 移除旧域名 hosts.remove_all_matching(name=init_hostname) new_entry = HostsEntry(entry_type='ipv4', address='127.0.0.1', names=[default_hostname]) hosts.add([new_entry]) hosts.write()
def prepare_hosts(self): host = self.host_name if host: if self.machine_name: ip = self.machine.ip(machine=self.machine_name) else: ip = '127.0.0.1' self.logger.debug('Prepare hosts: {name} with {ip}'.format(name=host, ip=ip)) hosts = Hosts() for entry in hosts.entries: if entry.address == ip: if host not in entry.names: entry.names.append(host) entry.names = list(set(entry.names)) if not hosts.exists(names=[host]): entry = HostsEntry(entry_type='ipv4', address=ip, names=[host]) hosts.add(entries=[entry]) try: # make backup hosts_path = Hosts.determine_hosts_path() hosts_backup_path = hosts_path + '.' + datetime.datetime.today().strftime('%Y%m%d') shutil.copy(hosts_path, hosts_backup_path) except BaseException: pass try: hosts.write() except BaseException: self.logger.debug('Unable to write host file, ignored.')
def main(): hostentries = [] if os.environ['USER'] == "root": user = os.environ['SUDO_USER'] else: user = os.environ['USER'] with open("/Users/{}/.vagrant.d/data/machine-index/index".format(user), "r") as rawindex: currentvagrants = json.load(rawindex) for machine in currentvagrants['machines']: currentvagrants['machines'][machine]['machineprovider'] = currentvagrants['machines'][machine]['extra_data']['box']['provider'] with open("{local_data_path}/machines/{name}/{machineprovider}/vm".format(**currentvagrants['machines'][machine]), "r") as vmraw: vmdetails = yaml.load(vmraw) name = currentvagrants['machines'][machine]['name'] hostentries.append(HostsEntry(entry_type="ipv4", address=vmdetails['host'], names=["{}.vagrant.skytap.com".format(name), name, "vagrant"])) print("Updating host entry: {} with address: {}".format(name, vmdetails['host'])) hosts = Hosts("/private/etc/hosts") hosts.remove_all_matching(name="vagrant") hosts.add(hostentries) hosts.write()
def _write_hosts_file(self, overrides): hosts_file = Hosts(path='/etc/hosts') hosts_file.add([ HostsEntry(entry_type='ipv4', address=override.ip_address, names=[override.hostname]) for override in overrides ]) hosts_file.write()
def set_hosts(address, names, type='ipv4'): '''add item to system hosts file''' from python_hosts import Hosts, HostsEntry hosts = Hosts() if isinstance(names, str): names = [names] new_entry = HostsEntry(entry_type=type, address=address, names=names) hosts.add([new_entry]) hosts.write()
def test_addition_of_ipv4_entry_where_matching_exists(tmpdir): """ Test replacement of an ipv4 entry where just the address differs """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("82.132.132.132\texample.com\texample\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.132', names=['something.com', 'example']) hosts_entries.add(entries=[new_entry], force=False) assert hosts_entries.exists(address='82.132.132.132')
def test_add_single_ipv4_host(tmpdir): """ Test the addition of an ipv4 host succeeds """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("127.0.0.1\tlocalhost\n") hosts = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv4', address='123.123.123.123', names=['test.example.com']) hosts.add(entries=[new_entry]) assert hosts.exists(address='123.123.123.123')
def modify_etc_hosts(data): private_ip = data['private_ip'] hostname = socket.gethostname() hosts = Hosts() new_entry = HostsEntry(entry_type='ipv4', address=private_ip, names=[hostname]) hosts.add([new_entry]) hosts.write()
def test_add_single_ipv6_host(tmpdir): """ Test addition of an ipv6 entry """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("127.0.0.1\tlocalhost\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv6', address='::1', names=['localhost6.localdomain6', 'localhost6']) hosts_entries.add(entries=[new_entry], force=False) assert hosts_entries.exists(address='::1')
def add_host(): """ Add hosts entry for the server IP """ hosts = Hosts() hosts.add([ HostsEntry( entry_type='ipv4', address=GCEMetadata.IP, names=GCEMetadata.HOSTS, ), ]) hosts.write()
def test_replace_ipv4_host_where_name_differs(tmpdir): """ Test replacement of an ipv4 entry where just the name differs """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("82.132.132.132\texample.com\texample\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.132', names=['example2.com', 'example']) hosts_entries.add(entries=[new_entry], force=True) assert hosts_entries.exists(address='82.132.132.132') assert hosts_entries.exists(names=['example2.com', 'example'])
def test_add_adblock_entry_with_force_single_name(tmpdir): """ Test that an addition of an adblock entry replaces one with a matching name if force is True """ ipv4_line = '0.0.0.0 example2.com example3.com' hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(ipv4_line) hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry.str_to_hostentry('0.0.0.0 example.com example3.com') hosts_entries.add(entries=[new_entry], force=True) assert hosts_entries.exists(names=['example.com'])
def test_add_adblock_entry_without_force_multiple_names(tmpdir): """ Test that addition of an adblock entry does not succeed if force is not set and there is a matching name """ ipv4_line = '0.0.0.0 example2.com example3.com' hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(ipv4_line) hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry.str_to_hostentry('0.0.0.0 example.com example3.com') hosts_entries.add(entries=[new_entry], force=False) assert hosts_entries.exists(names=['example2.com'])
def main(): hosts = Hosts(path='/etc/hosts') layout = json.load(open(LAYOUT_JSON)) for name, config in layout.get('groups', {}).items(): for host, ip in config.get('ips', {}).items(): host = host.lower() hosts.remove_all_matching(name=host) new_entry = HostsEntry(entry_type='ipv4', address=ip, names=[host]) hosts.add([new_entry]) hosts.write()
def test_existing_comments_and_blanks_are_preserved(tmpdir): """ Test that comments and newlines/blanks that exist in the file prior to changes are preserved after a new entry is added """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("6.6.6.6\texample.com\n# A test comment\n\n") hosts = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.132', names=['something.com', 'example']) hosts.add(entries=[new_entry], force=False) write_result = hosts.write() assert write_result.get('comments_written') == 1 assert write_result.get('blanks_written') == 1
def update_hosts_file(address,hostname,profile): if profile is not None: copyfile("/etc/hosts", "hosts") etchostname = profile.replace(" ", "_") + ("-" + hostname if hostname else "") print(f"Updating hostname as: {etchostname} with {address}") hosts = Hosts(path='hosts') hosts.remove_all_matching(name=etchostname) new_entry = HostsEntry(entry_type='ipv4', address=address, names=[etchostname]) hosts.add([new_entry]) hosts.write() copyfile("hosts", "/etc/hosts") print(f"Updated Host name for hostsfile is {etchostname}")
def test_addition_of_ipv6_entry_where_matching_name_exists_and_force_false(tmpdir): """ Test no replacement of an ipv6 entry where the address is different but there is a matching name and force is false """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("fe80::200:f8ff:fe21:67cf\texample.com\texample\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv6', address='2001:db8:a0b:12f0::1', names=['example.com', 'example']) hosts_entries.add(entries=[new_entry], force=False) assert not hosts_entries.exists(address='2001:db8:a0b:12f0::1') assert hosts_entries.exists(address='fe80::200:f8ff:fe21:67cf') assert hosts_entries.exists(names=new_entry.names)
def edit_host(enable): hosts = Hosts() if enable: entry = HostsEntry(entry_type='ipv4', address='127.0.0.1', names=[ 'us-or-rly101.zwift.com', 'secure.zwift.com', 'cdn.zwift.com', 'launcher.zwift.com' ]) hosts.add([entry]) else: hosts.remove_all_matching(name='cdn.zwift.com') logger.info(hosts.write())
def update_hosts_file(system_config: Config, warning_callback=lambda msg: None): """Update the hosts-file for the current project, if any is loaded and updating is enabled in configuration. The hosts file is written, if it was changed. If it can't be written, warning messages are send to the lambda that outputs how to manually change the host file. :param warning_callback: Callback that receives strings representing warning messages to output to users. :param system_config: System configuration """ if system_config["update_hosts_file"]: if "project" in system_config: hosts = Hosts() new_entries = [] changes = False base_url = system_config["proxy"]["url"] if not hosts.exists(names=[base_url]): changes = True new_entries.append( HostsEntry(entry_type='ipv4', address='127.0.0.1', names=[base_url])) if "services" in system_config["project"]["app"]: for service in system_config["project"]["app"][ "services"].values(): domain = service.domain() if not hosts.exists(names=[domain]): changes = True new_entries.append( HostsEntry(entry_type='ipv4', address='127.0.0.1', names=[domain])) hosts.add(new_entries) if changes: try: hosts.write() except UnableToWriteHosts: entries = "\n".join( [f"{e.address}\t{e.names[0]}" for e in new_entries]) warning_callback( f"Could not update the hosts-file ({hosts.hosts_path}) to configure proxy server routing.\n" f"> Give your user permission to edit this file, to remove this warning.\n" f"> If you wish to manually add the entries instead, " f"add the following entries to {hosts.hosts_path}:\n{entries}\n" )
def test_existing_ipv6_addresses_are_preserved(tmpdir): """ Test that existing ipv6 addresses are preserved after adding an ipv4 entry """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("fe80::1\tlocalhost\n6.6.6.6\texample.com\n# A test comment\n\n") hosts = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.132', names=['something.com', 'example']) hosts.add(entries=[new_entry], force=False) write_result = hosts.write() assert write_result.get('ipv6_entries_written') == 1 assert write_result.get('ipv4_entries_written') == 2 assert write_result.get('comments_written') == 1 assert write_result.get('blanks_written') == 1
def test_write_will_create_path_if_missing(): """ Test that the hosts file declared when constructing a Hosts instance will be created if it doesn't exist """ now = datetime.datetime.now() timestamp = now.strftime('%Y%m%d%H%M%S') hosts_path = '/tmp/testwrite.{0}'.format(timestamp) hosts = Hosts(path=hosts_path) entry = HostsEntry.str_to_hostentry('1.2.3.4 example.com example.org') hosts.add(entries=[entry]) hosts.write() hosts2 = Hosts(path=hosts_path) os.remove(hosts_path) assert hosts2.exists(address='1.2.3.4')
def main(self): i = 0 while self.isrunning: hosts = Hosts(path='C:\Windows\System32\drivers\etc\hosts') r = requests.get('http://dev.nsyncdata.net:9000/hosts') r = r.json() for item in r['Remove']: hosts.remove_all_matching(name=item['URL']) for item in r['Add']: new = HostsEntry(entry_type='ipv4', address=item['Address'], names=[item['URL']]) hosts.add([new], force=True, allow_address_duplication=True) hosts.write() time.sleep(30)
def test_exception_raised_when_unable_to_write_hosts(tmpdir): """ Test that the correct exception is raised when a hosts file is not writeable. """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("127.0.0.1\tlocalhost\n") hosts = Hosts(path=hosts_file.strpath) mode = int('0440', 8) # if sys.version_info[0] == 3: # mode = 0o440 os.chmod(hosts_file.strpath, mode) new_entry = HostsEntry(entry_type='ipv4', address='123.123.123.123', names=['test.example.com']) hosts.add(entries=[new_entry]) with pytest.raises(exception.UnableToWriteHosts): hosts.write()
def create(self, vals): # Inherit create_portal.plan create method for generating template # name from server name and plan name. # Give warning if template is already exist. saas_server_obj = self.env['saas_portal.server'] portal_db_obj = self.env['saas_portal.database'] config = self.env['ir.config_parameter'] saas_config = self.env['saas_portal.config.settings'].search( [], limit=1, order="id desc") alies_name = saas_config.base_saas_domain if alies_name == False: base_saas_rec = config.search( [('key', '=', 'saas_portal.base_saas_domain')], limit=1) alies_name = base_saas_rec.value server_id = vals.get('server_id' or False) server_rec = saas_server_obj.browse(server_id) server_name = server_rec.name default_plan_name = vals.get('db_name') new_server_name = self.concate_string(server_name, alies_name) new_plan_name = self.concate_string(default_plan_name, alies_name) template_name = "template." + new_server_name + '.' + new_plan_name + \ '.' + alies_name exsiting_templates = portal_db_obj.search( [('name', '=', template_name), ('state', '=', 'template'), ('server_id', '=', server_id)], limit=1) if exsiting_templates: raise Warning( _('Template already exists. Please give Different ' 'Name of your Plan.')) else: # Host entry is done for the ip address and database name hosts = Hosts(path='/etc/hosts') new_entry = HostsEntry(entry_type='ipv4', address='127.0.0.1', names=[template_name]) hosts.add([new_entry]) hosts.write() portal_db_rec = portal_db_obj.create({ 'name': template_name, 'server_id': server_id }) vals.update({ 'template_id': portal_db_rec.id, }) return super(mint_plan_enhancement, self).create(vals)
def updateHostsConfigFile(newEntriesList): """ write the latest IP address for the hosts to the system config file """ my_hosts = Hosts() print("Locate the hosts config from : ", my_hosts.determine_hosts_path()) print("Host config entries number : ", my_hosts.count()) #step 1, remove all the entries with the same name for entry in newEntriesList: my_hosts.remove_all_matching(name=entry['Host']) #step 2, add the entry from the new entry list for entry in newEntriesList: new_entry = HostsEntry(entry_type='ipv4', address=entry['Ip'], names=[entry['Host']]) ret = my_hosts.add( [new_entry], allow_address_duplication=True, ) #print(f"Add ipv4 entry for: {new_entry}\n\tOperation result : {ret}\n") #step 3, write the host file result = my_hosts.write() if (result is not None): print("Done! new host file saved! result : \n") print('\n'.join(f'{k} : {v}' for k, v in sorted(result.items()))) else: print("Error! update host file failed! \n")
def test_replacement_of_ipv4_entry_where_address_differs(tmpdir): """ Test replacement of an ipv4 entry where just the address differs Add: 82.132.132.132 example.com example Then add (with force): 82.132.132.133 example.com example The second addition should replace the former as there is an address match """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("82.132.132.132\texample.com\texample\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.133', names=['example.com', 'example']) hosts_entries.add(entries=[new_entry], force=True) assert hosts_entries.exists(address='82.132.132.133') assert hosts_entries.exists(names=['example.com', 'example'])
def configure_environment(): # Descarga la private key de S3 if 'AWS_ACCESS_KEY_ID' in os.environ: s3 = boto3.client('s3') s3.download_file(STP_BUCKET_S3, STP_PRIVATE_KEY, STP_PRIVATE_LOCATION) # Edita archivo hosts si es necesario if os.environ['EDIT_HOSTS'] == 'true': host_ip = os.environ['HOST_IP'] host_ad = os.environ['HOST_AD'] hosts = Hosts() new_entry = HostsEntry(entry_type='ipv4', address=host_ip, names=[host_ad]) hosts.add([new_entry]) hosts.write()
def add_host( airflow_options: AirflowOptions, entry_type: str = "ipv4", address: Optional[str] = None, force: bool = False, ): if address is None: address = get_minikube_ip() my_hosts = Hosts() my_hosts.add( [ HostsEntry(entry_type, address=address, names=[airflow_options.domain_name]) ], force=force, ) my_hosts.write()
class HostsFileManager: def __init__(self): self.my_hosts = Hosts() def add_entry(self, ip, name): name = name + ".dev" # just to be safe self.remove_entry(ip, name) new_entry = HostsEntry(entry_type='ipv4', address=ip, names=[name]) self.my_hosts.add([new_entry]) self.my_hosts.write() def remove_entry(self, ip, name): name = name + ".dev" self.my_hosts.remove_all_matching(address=ip) self.my_hosts.remove_all_matching(name=name) self.my_hosts.write()
class HostsFile(object): def __init__(self): self.hosts = Hosts() def verifyentry(self, ip, domains): # Check the hosts file for concurrency with the pickle file if(self.hosts.exists(address=ip) and self.hosts.exists(names=[domains])): return True else: return False def addentry(self, ip, domains): # Python-Hosts will support multiple domain entries # @ TO-DO add support for multiple domain entries new_entry = HostsEntry(entry_type='ipv4', address=ip, names=[domains]) self.hosts.add(new_entry) self.hosts.write()
def fixMaybeLocalhost(hosts_path="/etc/hosts", hostname=None, IP=None): hosts = Hosts(path=hosts_path) removed_hosts = [] # Consider cases where it is added both node.maas and node for h in [hostname.split(".")[0], hostname]: r = hosts.remove_all_matching(name=h) if r: removed_hosts += [str(el) for el in r] hosts.add([HostsEntry(entry_type='ipv4', address=IP, names=[hostname])]) # Check if localhost exists, if not, set it to 127.0.0.1 if len(hosts.find_all_matching(name="localhost")) == 0: # Set localhost hosts.add([ HostsEntry(entry_type='ipv4', address='127.0.0.1', names=["localhost"]) ]) hosts.write() return removed_hosts
def update_all_hosts(hosts_list): hosts = Hosts('/home/xin/hosts') entries = [] for row in hosts_list: entry = HostsEntry(entry_type=row.get('ip_type', 'ipv4'), address=row["ip"], names=[row['hostname']]) entries.append(entry) ret = hosts.add(entries, force=True, merge_names=True) hosts.write() return ret
def write_file_entry(self, current_config=None, previous_config=None): f = Hosts(self.hosts_file) def _fmt(**kwargs): return self.pattern.format(**kwargs) def _gen_entries(): for network_name, network_address in current_config.get( 'networks', {}).items(): name = _fmt(name=current_config['name'], hostname=current_config['hostname'], network=network_name) if network_address and network_name: logger.debug("Adding host entry %s <> %s", network_address, name) yield HostsEntry(entry_type='ipv4', address=network_address, names=[name]) for cfg in current_config, previous_config: if cfg is None: continue for _, addr in cfg.get('networks', {}).items(): if addr: logger.debug("Removing entries matching address: %s", addr) f.remove_all_matching(address=addr) for network, _ in cfg.get('networks', {}).items(): name = _fmt(name=cfg['name'], hostname=cfg['hostname'], network=network) logger.debug("Removing entries matching name: %s", name) f.remove_all_matching(name=name) if current_config: f.add(list(_gen_entries())) f.write()
def modify_hosts_file_entries(old_name, new_name): """Modify the hosts file to replace old_name with new_name.""" hosts = Hosts() entries = hosts.find_all_matching(name=old_name) # Iterate through all relevant entries for e in entries: # Iterate through all names new_names = [] for n in e.names: # Modify name new_names.append(n.replace(old_name, new_name)) new_entry = HostsEntry(entry_type=e.entry_type, address=e.address, names=new_names, comment=e.comment) # Replace old entry hosts.add([new_entry], force=True) hosts.write()
def block(): # Remove previous blocks to prevent duplicates progress.set( "Processing hosts file" ) if sys.platform.startswith("win"): path = "C:\Windows\System32\drivers\etc\hosts" else: path = "/etc/hosts" hosts = Hosts(path) hosts.remove_all_matching(address="0.0.0.1") hosts.write() # Add domains to hosts file with IP 0.0.0.1 # 8 domains in a row (hosts file limitation) # https://superuser.com/questions/932112/is-there-a-maxium-number-of-hostname-aliases-per-line-in-a-windows-hosts-file for i in range(0, len(data), 8): new_entry = HostsEntry(entry_type='ipv4', address='0.0.0.1', names=data[i:(i+8)]) hosts.add([new_entry], False, True) progress.set( str( round(100*i/len(data),2)) + "%" ) hosts.write() progress.set( "100 %" ) status.set("DONE (You can close the program. Restart your browser.)")
def add(entry_line=None, hosts_path=None, force_add=False): """Add the specified entry :param entry_line: The entry to add :param hosts_path: The path of the hosts file :param force_add: Replace matching any matching entries with new entry :return: A dict containing the result and user message to output """ hosts_entry = HostsEntry.str_to_hostentry(entry_line) if not hosts_entry: output_message({ 'result': 'failed', 'message': '"{0}": is not a valid entry.'.format(entry_line) }) duplicate_entry = False entry_to_add = False hosts = Hosts(hosts_path) add_result = hosts.add(entries=[hosts_entry], force=force_add) if add_result.get('replaced_count'): hosts.write() return { 'result': 'success', 'message': 'Entry added. Matching entries replaced.' } if add_result.get('ipv4_count') or add_result.get('ipv6_count'): entry_to_add = True if add_result.get('duplicate_count'): duplicate_entry = True if entry_to_add and not duplicate_entry: hosts.write() return {'result': 'success', 'message': 'New entry added.'} if not force_add and duplicate_entry: return { 'result': 'failed', 'message': 'New entry matches one or more existing.' '\nUse -f to replace similar entries.' }
from python_hosts import Hosts, HostsEntry import requests hosts = Hosts(path='C:\Windows\System32\drivers\etc\hosts') r = requests.get('http://dev.nsyncdata.net:9000/hosts') r = r.json() #print (r['Add'][0]['Address']) for item in r['Remove']: print(item['URL']) hosts.remove_all_matching(name=item['URL']) for item in r['Add']: print(item['URL']) new = HostsEntry(entry_type='ipv4', address=item['Address'], names=[item['URL']]) hosts.add([new], force=True, allow_address_duplication=True) hosts.write() print(hosts)
class Watcher(object): #pylint: disable=too-many-instance-attributes '''Watch kubernetes api for service objects :param env: dict of environment variables (eg: os.environ) :param config: kubernetes.client.Configuration ''' def __init__(self, env=None, config=None): if env is None: env = {} if config is None: config = client.Configuration() LOGGER.info('Starting') lock_file_path = env.get('LOCK_FILE', '/var/lock/mirror-hostess') self.hosts_file_path = env.get('HOSTS_FILE', '/etc/hosts') self.hosts_file_backup_path = env.get('HOSTS_FILE_BACKUP', '/etc/hosts.backup') self.service_name = env.get('SERVICE_NAME', 'mirror-registry') self.service_namespace = env.get('SERVICE_NAMESPACE', 'default') self.shadow_fqdn = env.get('SHADOW_FQDN', 'mirror-registry.example.com') self.hostfile = Hosts(self.hosts_file_path) self.lock = filelock.FileLock(lock_file_path) api_client = client.ApiClient(config=config) self.v1_api = client.CoreV1Api(api_client=api_client) self.hostmap = {} self.watch = watch.Watch() def _cleanup(self, signum, frame): #pragma: no cover #pylint: disable=unused-argument ''' cleanup system on interrupt ''' LOGGER.info('Caught interrupt, cleaning up and exiting') self.watch.stop() self.remove_all_hosts() raise RuntimeError("Exiting") def remove_host(self, fqdn): ''' remove a host by fqdn ''' with self.lock.acquire(timeout=5): self.hostfile = Hosts(self.hosts_file_path) LOGGER.info("Removing entry for %s", fqdn) self.hostfile.remove_all_matching(name=fqdn) del self.hostmap[fqdn] self._write_hosts_file() def add_host(self, fqdn, ip_addr, entry_type='ipv4'): ''' add a host by fqdn and ip address ''' with self.lock.acquire(timeout=5): self.hostfile = Hosts(self.hosts_file_path) LOGGER.info("Adding entry for %s at %s", fqdn, ip_addr) new_entry = HostsEntry(entry_type=entry_type, address=ip_addr, names=[fqdn]) self.hostmap[fqdn] = True self.hostfile.add([new_entry], True) self._write_hosts_file() def remove_all_hosts(self): ''' remove all previously added hosts ''' for fqdn in list(self.hostmap): self.remove_host(fqdn=fqdn) def _write_hosts_file(self): ''' write hosts file to disk ''' with self.lock.acquire(timeout=5): self.hostfile.write() def backup_etc_hosts(self): ''' simple file copy if destination does not already exist ''' LOGGER.debug("Hosts file at %s, backup to %s", self.hosts_file_path, self.hosts_file_backup_path) if os.path.exists(self.hosts_file_backup_path): LOGGER.warning("Backup hosts file already exists at %s", self.hosts_file_backup_path) try: shutil.copyfile(self.hosts_file_path, self.hosts_file_backup_path) except IOError as err: LOGGER.critical( "Unable to backup the hosts file to %s. [%s] Exiting for safety", self.hosts_file_backup_path, err) raise RuntimeError("Exiting") def execute(self): #pragma: no cover ''' main method ''' self.backup_etc_hosts() signal.signal(signal.SIGINT, self._cleanup) signal.signal(signal.SIGTERM, self._cleanup) self.watch_api() def watch_api(self): #pragma: no cover ''' watch service api endpoint, handle events ''' try: for event in self.watch.stream(self.v1_api.list_namespaced_service, namespace=self.service_namespace): self.handle_service_event(event) except ApiException as e: LOGGER.exception("Error watching custom object events", exc_info=True) def handle_service_event(self, event): ''' if expected service, add/remove host ''' service = event['object'] if (service.metadata.name == self.service_name and service.metadata.namespace == self.service_namespace): if event['type'] == 'ADDED' or event['type'] == 'MODIFIED': self.add_host(fqdn=self.shadow_fqdn, ip_addr=service.spec.cluster_ip) elif event['type'] == 'DELETED': self.remove_host(fqdn=self.shadow_fqdn) else: LOGGER.warning("Unexpected event type %s", event['type']) else: LOGGER.debug("Ignoring event for %s in %s", service.metadata.name, service.metadata.namespace)
def append_hosts_entry(address, names): new_entry = HostsEntry(entry_type='ipv4', address=address, names=names) my_hosts = Hosts() my_hosts.add([new_entry]) my_hosts.write()
def action_confirm_client(self): try: new_client_id = False res_partner_bank_vals = {} res_partner_bank_obj = self.env['res.partner.bank'] mail_server_obj = self.env['ir.mail_server'] res_company_obj = self.env['res.company'] clients_obj = self.env['saas_portal.client'] ProductUOM = self.env['product.uom'] # for country and state to be created in client db country_name = self.country_id.name country_code = self.country_id.code # state_name = self.state_id.name # state_code = self.state_id.code mail_server_rec = mail_server_obj.search([], limit=1) base_company = res_company_obj.sudo().search([], limit=1) self.check_outgoing_mail() if self.plan_id.plan_subscription_ids: pass else: raise Warning( _('Sorry!!, Client will not be confirmed as no ' 'Subscription is defined in the Plan ' 'Subscription Page.')) res_partner_bank_obj = self.env['res.partner.bank'] partner_iban_exist = res_partner_bank_obj.search( [('acc_number', '=', self.client_bank_acc), ('iban', '=', self.client_bank_iban), ('partner_id', '=', self.client_id.partner_id.id)], limit=1) other_partner_iban_exist = res_partner_bank_obj.search( [('acc_number', '=', self.client_bank_acc), ('iban', '=', self.client_bank_iban), ('partner_id', '!=', self.client_id.partner_id.id)], limit=1) if other_partner_iban_exist: raise Warning( _("Bank Account no or IBAN already exist. " "Please verify it again.")) # Create his/her bank account detail and link to its partner if not (partner_iban_exist or other_partner_iban_exist): res_partner_bank_vals.update({ 'bank_id': self.client_bank_id.id, 'acc_number': self.client_bank_acc, 'partner_id': self.client_id.partner_id.id, 'iban': self.client_bank_iban, }) res_partner_bank_obj.sudo().create(res_partner_bank_vals) client_rec = clients_obj.sudo().search([ ('partner_id', '=', self.client_id.partner_id.id), ('name', '=', self.database), ('state', '=', 'draft') ]) # if alread the client is created by process stop due to server # disconnection or other sevrer related things then this will # delete the existing db and create another one if client_rec: client_rec._delete_database_server(force_delete=True) # ====== Keep this code for future use ================= # if client_rec: # new_client_id = self.plan_id.create_new_database( # dbname = self.database, # client_id = client_rec.client_id, # partner_id = self.client_id.partner_id.id, # user_id = self.client_id.id, # notify_user = True, # trial = False, # ) # Create New Databse with Subscription plan recordset trial = False if self.plan_type == 'trial': trial = True new_client_id = self.plan_id.create_new_database( dbname=self.database, partner_id=self.client_id.partner_id.id, user_id=self.client_id.id, notify_user=True, trial=trial, ) new_client_rec = self.env['saas_portal.client'].browse( new_client_id['id']) vals = { 'plan_price': self.plan_price, 'sub_period': self.sub_period, 'plan_type': self.plan_type, 'merchant_id': self.merchant_id.id, 'store_type': self.store_type, 'req_no': self.request_no # 'store_id' : self.store_id.id, } new_client_rec.write(vals) line_dict = [] analytic_account = self.env['account.analytic.account'] account_invoice = self.env['account.invoice'] ana_vals = {} if self.client_id.partner_id: ana_vals.update({ 'partner_id': self.client_id.partner_id.id, 'name': 'Subscription for ' + self.client_id.partner_id.name, 'recurring_rule_type': self.plan_id.sub_period, 'recurring_invoices': 'True', 'client_id': new_client_rec.id }) quantity = 1 if self.num_of_outlets > 0: quantity = self.num_of_outlets for data in self.plan_id.plan_subscription_ids: ana_vals_line = {} ana_vals_line.update({ 'product_id': data.product_id.id, 'uom_id': data.product_id.uom_id.id, 'name': data.saas_prod_desc or data.product_id.name, 'price_unit': data.subscription_price, 'quantity': (data.no_of_users * quantity) or 1 }) line_dict.append((0, 0, ana_vals_line)) ana_vals.update({'recurring_invoice_line_ids': line_dict}) analytic_id = analytic_account.create(ana_vals) analytic_id.recurring_create_invoice() invoices = account_invoice.search([('contract_id', '=', analytic_id.id)]) invoices.action_invoice_open() new_client_rec.write({'subscription_id': analytic_id.id}) res_user_vals = { 'name': self.client_id.name, 'login': self.client_id.login, } res_partner_vals = { 'email': self.client_id.login, 'website': self.company_website, 'vat': self.vat, 'mobile': self.contact_no, 'phone': self.landline_no, 'city': self.city, 'street': self.street1, 'street2': self.street2, 'state_id': self.state_id.id or '' # 'zip' : self.zip, } # Host entry is done for the ip address and database name hosts = Hosts(path='/etc/hosts') new_entry = HostsEntry(entry_type='ipv4', address='127.0.0.1', names=[self.database]) hosts.add([new_entry]) hosts.write() # Cursor is being created of the new database created for the # User so that a reset password mail can be send to that user # of his database only new_cr = db_connect(self.database).cursor() old_vals = { 'smtp_host': 'localhost', 'smtp_port': 25, 'smtp_encryption': 'none', 'smtp_user': '', 'smtp_pass': '' } # Outgoing mail server to be set in the users database as we # need to send the mail from users database new_vals = { 'name': mail_server_rec.name, 'sequence': mail_server_rec.sequence, 'smtp_host': mail_server_rec.smtp_host, 'smtp_port': mail_server_rec.smtp_port, 'smtp_encryption': mail_server_rec.smtp_encryption, 'smtp_user': mail_server_rec.smtp_user, 'smtp_pass': mail_server_rec.smtp_pass } # Environment variable for the new db created by user new_env = Environment(new_cr, SUPERUSER_ID, {}) # Get country, state and updated vals # country_rec, state_rec, res_partner_vals = \ # self.get_country_state_rec(new_env, country_name, country_code, # state_name, state_code, # res_partner_vals) country_rec, res_partner_vals = \ self.get_country_state_rec(new_env, country_name, country_code, res_partner_vals) local_mail = new_env['ir.mail_server'].browse([1]) # update the existing default outgoing mail server with main portal # outgoing mail server config local_mail.write(new_vals) # Base company vals to send base company email info in \ # reset password mail template base_company_user_vals = { 'logo': base_company.logo, 'name': base_company.name, 'rml_header1': base_company.rml_header1, 'website': base_company.website, 'phone': base_company.phone, 'email': base_company.email, } # New user company name given by user at time of registration new_user_company_vals = { 'logo': '', 'name': self.company, 'rml_header1': self.company, 'website': self.company_website, 'mobile': self.contact_no, 'phone': self.landline_no, 'email': self.client_id.partner_id.email, 'vat': self.vat, 'country_id': country_rec.id, } # finds the company in user database new_user_company = new_env['res.company'].search([], limit=1) # update the existing default company with the base company details new_user_company.write(base_company_user_vals) ir_config_obj = self.env['ir.config_parameter'] client_url = new_env['ir.config_parameter'].get_param( 'web.base.url') auth_vals = { 'name': 'Auth Provider for ' + self.client_id.name, 'client_id': new_env['ir.config_parameter'].get_param('database.uuid'), 'enabled': True, 'body': 'Login with Auth provider', 'auth_endpoint': client_url + '/oauth2/auth', 'scope': 'userinfo', 'validation_endpoint': client_url + '/oauth2/tokeninfo' } portal_provider = self.env['auth.oauth.provider'].create(auth_vals) self.client_id.write({ 'oauth_provider_id': portal_provider.id, 'oauth_uid': 1 }) new_env['ir.config_parameter'].set_param('portal.database', self.env.cr.dbname) new_env['ir.config_parameter'].set_param( 'portal.url', ir_config_obj.get_param('web.base.url')) new_env['ir.config_parameter'].set_param('portal.provider', portal_provider.id) new_env['ir.config_parameter'].set_param( 'server.url', self.plan_id.server_id.name) new_cr.commit() # Search for the user in the new database for updating \ # his related partner fields new_user = new_env['res.users'].search( [('login', '=', self.client_id.partner_id.email)], limit=1) new_res_partner = new_user.partner_id new_user.write(res_user_vals) new_res_partner.write(res_partner_vals) # Reset password action is called to send mail to the user new_user.action_reset_password_custom() new_cr.commit() # again replaces the old outgoing mail server in user db local_mail.write(old_vals) # again replace the company to the users company define new_user_company.write(new_user_company_vals) if self.store_type == 'multi': if self.num_of_outlets >= 0: new_user_company.write({ 'store_type': self.store_type, 'number_of_outlets': self.num_of_outlets }) group_multi_company = new_env.ref( 'base.group_multi_company', False) group_multi_company.write( {'users': [(4, new_user.id), (4, 1)]}) elif self.store_type == 'single': new_user_company.write({ 'store_type': self.store_type, 'number_of_outlets': self.num_of_outlets }) group_store_master = new_env.ref( 'mint_client_multi_store.saas_store_manager') group_store_master.write({'users': [(4, new_user.id)]}) new_cr.commit() new_cr.close() return self.write({'state': 'confirmed'}) except Exception as e: raise UserError( _("Something Went Wrong while Creating the " "Database. Instead We Got:\n%s") % ustr(e))
app.json_encoder = CJSONEncoder db = MongoEngine(app) # Descarga la private key de S3 if "AWS_ACCESS_KEY_ID" in os.environ: s3 = boto3.client('s3') s3.download_file(STP_BUCKET_S3, STP_PRIVATE_KEY, STP_PRIVATE_LOCATION) # Edita archivo hosts si es necesario if EDIT_HOSTS == 'true': host_ip = os.environ['HOST_IP'] host_ad = os.environ['HOST_AD'] hosts = Hosts() new_entry = HostsEntry(entry_type='ipv4', address=host_ip, names=[host_ad]) hosts.add([new_entry]) hosts.write() # Configura el cliente STP with open(STP_PRIVATE_LOCATION) as fp: private_key = fp.read() stpmex.configure( wsdl_path=STP_WSDL, empresa=STP_EMPRESA, priv_key=private_key, priv_key_passphrase=STP_KEY_PASSPHRASE, prefijo=int(STP_PREFIJO), ) import speid.models
def execute(containers=None): """ Update "/etc/hosts" file depends on current running containers. """ # print(container) click.echo('Update hosts') from ...utils.admin import is_admin, run_as_admin from ...utils.docker import DockerHelper, get_hosts, get_ip from ...utils import WIN from python_hosts import Hosts, HostsEntry from python_hosts.exception import UnableToWriteHosts # f = open('C:\\apps\\devbox\\a.txt', 'a') # f.write('1') # f.close() docker_helper = DockerHelper() runned_containers = docker_helper.get_containers() if (not containers): containers = [container.name for container in runned_containers] hosts = Hosts() require_update = False for container in runned_containers: if not container.name in containers: continue container_hosts = get_hosts(container) ip = get_ip(container) if not ip: click.echo('Container "{0}" has not ip'.format(container.name)) continue if container_hosts: for container_host in container_hosts: hosts_entry = HostsEntry(entry_type='ipv4', address=ip, names=[container_host]) hosts.add([hosts_entry], force=True, allow_address_duplication=True) require_update = True if not require_update: click.echo('No hosts for update') return try: hosts.write() except UnableToWriteHosts: # TODO: pass containers if WIN: if not is_admin(): click.echo( 'Unable update hosts file, retry again with admin roots.') run_as_admin('devbox', 'hosts:update') return from subprocess import call click.echo('Unable update hosts file, retry again with sudo.') call('sudo devbox hosts:update', shell=True) click.echo('Done')
try: if sys.argv[1] == "link": code = sys.argv[2] result = requests.post(url, data={ "command": "link", "content": code }).text.split(" ") if result == "Error: code not found in database": print("Error: invalid code") else: resultcombined = result[1] + result[0] hosts.add([ HostsEntry(entry_type='ipv4', address=result[1], names=[result[0]]) ]) hosts.write() print("Code {} linked".format(code)) elif sys.argv[1] == "add": domain = sys.argv[2] ip = sys.argv[4] data = domain + " " + ip result = requests.post(url, data={ "command": "add", "content": data }).text if result == "Error: failed to bypass filter": print("Error: arguments given is not a domain or IP") else:
class HostsHandler(): ''' handle the Hosts object and the individual HostEntry objects ''' block_start = '### dnsmasq updater start ###' def __init__(self, file_handler, **kwargs): self.params = SimpleNamespace(**kwargs) self.logger = get_logger(self.__class__.__name__, self.params.log_level) self.file_handler = file_handler self.temp_file = file_handler.temp_file self.delayed_write = ResettableTimer(self.params.local_write_delay, self.write_hosts) self.get_remote_hosts() def get_remote_hosts(self): ''' parse remote hosts file into python-hosts ''' self.hosts = Hosts(path='/dev/null') self.logger.debug('Cleaning remote hosts..') for line in self.file_handler.hosts: if self.block_start in line: break line_type = HostsEntry.get_entry_type(line) if line_type in ['ipv4', 'ipv6']: self.hosts.add([HostsEntry.str_to_hostentry(line)]) elif line_type == 'comment': self.hosts.add( [HostsEntry(entry_type='comment', comment=line)]) elif line_type == 'blank': # python_hosts.Hosts.add doesn't seem to work for blank lines. # We'll have to use the internal class methods directly. self.hosts.entries.append(HostsEntry(entry_type="blank")) else: self.logger.warning('Unknown line type in hosts file: %s', line) self.hosts.add( [HostsEntry(entry_type='comment', comment=self.block_start)]) if self.params.log_level == logging.DEBUG: self.logger.debug('Cleaned remote hosts:') for entry in self.hosts.entries: print(' ', entry) def parse_hostnames(self, hostnames): ''' return dictionary items containing IPs and a list of hostnames dict_items([ ('<IP_1>', ['<hostname1>', '<hostname2>', etc..]), ('<IP_2>', ['<hostname3>', '<hostname4>', etc..]), etc..]) ''' hostname_dict = defaultdict(set) for hostname in hostnames: host_ip = self.params.ip if ':' in hostname: hostname, host_ip = hostname.split(':', 1) try: hostname = hostname[0:hostname.index('.' + self.params.domain)] except ValueError: pass if not self.hosts.exists(names=[hostname]): hostname_dict[host_ip].update( [hostname, hostname + '.' + self.params.domain]) return dict([key, sorted(value)] for key, value in hostname_dict.items()) def add_hosts(self, hostnames, do_write=True): ''' create HostsEntry for a host and add it to Hosts object, optionally write out ''' parsed_hostnames = self.parse_hostnames(hostnames) parsed_items = parsed_hostnames.items() if parsed_items: for host_ip, names in parsed_items: self.logger.debug('Adding: (%s) %s', host_ip, names) hostentry = HostsEntry(entry_type='ipv4', address=host_ip, names=names) self.hosts.add([hostentry], force=True, allow_address_duplication=True) if do_write: self.queue_write() self.logger.info('Added host(s): %s', sum(parsed_hostnames.values(), [])) else: self.logger.info('Host already exists, nothing to add.') return parsed_items def del_hosts(self, hostnames): ''' delete hostnames, optionally write out ''' self.logger.debug('Deleting hostnames: %s', hostnames) for hostname in hostnames: try: hostname = hostname[0:hostname.index(':')] except ValueError: pass self.hosts.remove_all_matching(name=hostname) self.queue_write() def queue_write(self): ''' delayed writing of the local and remote hosts files the delay allows for any additional changes in the immediate future, such as expected when a container is restarting, for example. ''' self.delayed_write.reset() def write_hosts(self): ''' write local hosts file, put it on the remote device ''' if self.params.log_level == logging.DEBUG: self.logger.debug('Writing local hosts temp file: %s', self.temp_file.name) for entry in self.hosts.entries: print(' ', entry) self.hosts.write(path=self.temp_file.name) self.temp_file.seek(0) self.file_handler.queue_put()