'KXc@soddlmZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z ddl m Z id d gd 6d d gd 6dgd6dgd6ddgd6dgd6dgd6ZejeZdefdYZee jdZdZdZedZdZeedZdZdS(i(tStringION(timporter(tlog(tssh_util(t type_utils(tutil(thoststdebiantubuntutfedoratrheltredhattgentootfreebsdtopensusetslestsusetarchtaixtDistrocBseZejZdZdZdZdZdgZ dZ ej dZ ej dZ dZd Zd*d Zd*d Zej d*d*d Zej d ZdZd*dZd*d*dZedZej d*dZej dZdZej d*dZej dZej dZdZ ej dZ!e"dZ#dZ$dZ%dZ&dZ'dZ(d Z)d!Z*d"Z+d#Z,d$Z-e.d%Z/d&d'Z0d*d(Z1d)Z2RS(+s /etc/hostss"/etc/sudoers.d/90-cloud-init-userss /etc/hostnames/usr/share/zoneinfotservicecCs||_||_||_dS(N(t_pathst_cfgtname(tselfRtcfgtpaths((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt__init__=s  cCs tdS(N(tNotImplementedError(Rtpkglist((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pytinstall_packagesBscCs tdS(N(R(Rtsettings((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt_write_networkFscCsMtjj|jt|}tjj|sItd||fn|S(Ns(Invalid timezone %s, no file found at %s(tostpathtjoint tz_zone_dirtstrtisfiletIOError(Rttzttz_file((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt _find_tz_fileLs cCs|jS(N(tinit_cmd(R((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt get_init_cmdSscCs|jj||S(N(Rtget(Rtopt_nametdefault((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt get_optionVscCs6|j||}|j||j|j|dS(N(t_select_hostnamet_write_hostnamethostname_conf_fnt_apply_hostname(Rthostnametfqdntwriteable_hostname((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt set_hostnameYscCs tdS(N(R(Rtcmdtargstpkgs((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pytpackage_command^scCs tdS(N(R(R((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pytupdate_package_sourcesbscCs!tjd}|dkrdS|S(Niti386ti486ti586ti686(R>R?R@RA(R!tuname(RR((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pytget_primary_archfs  cCs4|jdg}|s'|j}nt||S(Ntpackage_mirrors(R0RCt_get_arch_package_mirror_info(RRt mirror_info((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyRElscCs"|j|}td|d|S(Ntavailability_zoneRF(REt_get_package_mirror_info(RRRGt arch_info((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pytget_package_mirror_infors cCs3|j|}|r/|j||j|StS(N(R t_bring_down_interfacest_bring_up_interfacestFalse(RRtbring_upt dev_names((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt apply_networkzs   cCs tdS(N(R(Rtlocaletout_fn((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt apply_localescCs tdS(N(R(RR(((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt set_timezonescCsdS(Ns 127.0.0.1((R((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt_get_localhost_ipscCs tdS(N(R(RtfilenameR/((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt_read_hostnamescCs tdS(N(R(RR5RV((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyR2scCs tdS(N(R(R((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt_read_system_hostnamescCsUtjd|ytjd|gWn'tjk rPtjtd|nXdS(Ns2Non-persistently setting the system hostname to %sR5s;Failed to non-persistently adjust the system hostname to %s(tLOGtdebugRtsubptProcessExecutionErrortlogexc(RR5((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyR4s  cCs tdS(N(R(RR5R6((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyR1scCsKg}x>|D]6}|tkr2td|n|jt|q W|S(Ns%No distibutions found for osfamily %s(t OSFAMILIESt ValueErrortextend(t family_listtdistrostfamily((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pytexpand_osfamilys   c Cs|}|j||}|rBtjj|rB|j|}nd}|j\}}g}| ss||kr|j|n| s||kr||kr|j|ntg|D]} | r| ^q}t j d|t |xL|D]D} y|j || Wqt k r:tjt d|| qXqW|rm|rm||krmt j d||n||kr|j|ndS(Ns/Attempting to update hostname to %s in %s filess!Failed to write hostname %s to %ss6%s differs from %s, assuming user maintained hostname.(R1R!R"texistsRWtNoneRXtappendtsetRYRZtlenR2R'RR]R4( RR5R6tprev_hostname_fntapplying_hostnamet prev_hostnametsys_fnt sys_hostnamet update_filestftfn((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pytupdate_hostnames6 %        c Cs"d}tjj|jr9tjtj|j}n!tjd}tjdd}|j }|j |}t }|s|j |||t }n%t }x|D]}d} g} t|dkr|d} nt|dkr|d} n| dk r| |kr|| kr*t }q*qqW|rt|} | j||g|j|x_| D]T}t|dkr|j ||dqgt|dkrg|j ||qgqgWn|rt} |r| jd|n| jd|tj|j| jdd ndS( Nttbasetaddediiis%s tmodei(R!R"Rethosts_fnRt HostsConfRt load_filet make_headerRUt get_entryRMt add_entrytTrueRfRitlistRgt del_entriesRtwritet write_filetgetvalue( RR5R6theadertehtlocal_ipt prev_infot need_changetentryt entry_fqdnt entry_aliasest new_entriestcontents((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pytupdate_etc_hostssJ          cCsd|g}tjd||y?tj|\}}t|rYtjd||ntSWn(tjk rtjtd|t SXdS(Ntifups8Attempting to run bring up interface %s using command %ss(Running %s resulted in stderr output: %ss#Running interface command %s failed( RYRZRR[RitwarnR}R\R]RM(Rt device_nameR9t_outterr((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt_bring_up_interfaces    cCsGd}x*|D]"}|j|s |d7}q q W|dkrCtStS(Nii(RR}RM(Rt device_namest am_failedtd((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyRL#s  cCsd|g}tjd||y?tj|\}}t|rYtjd||ntSWn(tjk rtjtd|t SXdS(Ntifdowns:Attempting to run bring down interface %s using command %ss(Running %s resulted in stderr output: %ss#Running interface command %s failed( RYRZRR[RiRR}R\R]RM(RRR9RR((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt_bring_down_interface,s    cCsGd}x*|D]"}|j|s |d7}q q W|dkrCtStS(Nii(RR}RM(RRRR((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyRK9s  cCs |jdS(Nt default_user(R0(R((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pytget_default_userBsc Kstj|r$tjd|dSd|g}d|g}i dd6dd6dd 6d d 6d d 6dd6dd6dd6dd6}idd6dd6dd6}d g}x|jD]\}} ||kr6| r6t| tr6|j||| g||kr|j||dgqm|j||| gq||kr| r|j|||j||qqWd|ksd|kr|jd|jdn|jd|jdtj d |ytj |d!|Wn,t k r} tj td"|| nXdS(#sC Add a user to the system using standard GNU tools s!User %s already exists, skipping.Ntuseradds --commenttgecoss--homethomedirs--gidt primary_groups--groupstgroupss --passwordtpasswds--shelltshells --expiredatet expiredates --inactivetinactives--selinux-usert selinux_users--no-user-groupt no_user_groups--systemtsystems --no-log-initt no_log_inittREDACTEDtno_create_homes-Ms-msAdding user %st logstringsFailed to create user %s( Rtis_userRYtinfot iteritemst isinstanceR%R`RgRZR[t ExceptionR]( RRtkwargst adduser_cmdtlog_adduser_cmdt adduser_optst adduser_flagst redact_optstkeytvalte((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pytadd_userEsP     !    cKs|j||d|kr=|dr=|j||dn|jdtr_|j|nd|kr|j||dnd|krt|dpg}tj||ddntS(s Creates users for the system using the GNU passwd tools. This will work on an GNU system. This should be overriden on distros where useradd is not desirable or not available. tplain_text_passwdt lock_passwdtsudotssh_authorized_keystoptionsN( Rt set_passwdR-R}Rtwrite_sudo_rulesRhRtsetup_user_keysRf(RRRtkeys((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt create_users  cCsMytjdd|gWn,tk rH}tjtd||nXdS(sL Lock the password of a user, i.e., disable password logins Rs-ls&Failed to disable password for user %sN(RR[RR]RY(RRR((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyRs cCsd||f}dg}|r/|jdnytj||dd|Wn,tk r{}tjtd||nXtS(Ns%s:%stchpasswds-eRschpasswd for %ssFailed to set password for %s(RgRR[RR]RYR}(RtuserRthashedt pass_stringR9R((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyRs  s /etc/sudoersc Csd}t}tjj|r6tj|}t}nt}x|jD]y}|j}t j d|}|syqIn|j dj}|sqIntjj |}||krIt}PqIqIW|sy|sddtj ddd|dg} dj| }tj||d nAdtj ddd|dg} dj| }tj||tjd ||fWqtk r} tjtd || qXntj|d dS( NRss^#includedir\s+(.*)$is?# See sudoers(5) for more information on "#include" directives:RtRus#includedir %ss i sAdded '#includedir %s' to %ssFailed to write %si(RMR!R"ReRRyR}t splitlineststriptretsearchtgrouptabspathRzR#Rt append_fileRYRZR'R]t ensure_dir( RR"t sudo_basetsudoers_contentst base_existst found_includetlinet include_matcht included_dirtlinesR((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pytensure_sudo_dirsH    c Cs|s|j}ndd|g}t|ttfrbxv|D]}|jd||fq>WnNt|ttfr|jd||fnd}t|tj |dj |}|d7}|j t j j|t j j|sXtj|g}y tj|dj |dWqtk rT} tjtd|| qXnCytj||Wn,tk r} tjtd|| nXdS( NRss# User rules for %ss%s %ss1Can not create sudoers rule addition with type %rs i sFailed to write sudoers file %ss Failed to append sudoers file %s(t ci_sudoers_fnRR~ttupleRgt basestringR%t TypeErrorRtobj_nameR#RR!R"tdirnameReRRzRR'R]RYR( RRtrulest sudo_fileRtruletmsgtcontentRR((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyRs6        cCsd|g}tj|r/tjd|nFy"tj|tjd|Wn!tk rttjd|nXt|dkrxl|D]a}tj |stjd||qntjddd ||gtjd ||fqWndS( Ntgroupadds(Skipping creation of existing group '%s'sCreated new group %ssFailed to create group %sisCUnable to add group member '%s' to group '%s'; user does not exist.tusermods-as-GsAdded user '%s' to group '%s'( Rtis_groupRYRR[RRR]RiR(RRtmemberst group_add_cmdtmember((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyt create_groups       N(3t__name__t __module__tabctABCMetat __metaclass__RwRR3R$R+RtabstractmethodRR R*R,RfR0R8R<R=RCRERJR}RPRSRTRURWR2RXR4R1t staticmethodRdRrRRRLRRKRRRRRMRRRR(((sG/opt/freeware/lib/python2.7/site-packages/cloudinit/distros/__init__.pyR4s\              6 +  C   ) %c Cs8|si}ndd}i}|r2||ds:           &  9 k I