Example #1
0
 def __init__(self, logger):
     self.logger = logger
     try:
         waagent.MyDistro = waagent.GetMyDistro()
         Config = waagent.ConfigurationProvider(None)
     except Exception as e:
         errorMsg = "Failed to construct ConfigurationProvider, which may due to the old wala code."
         self.logger.log(errorMsg)
         Config = waagent.ConfigurationProvider()
     self.proxyHost = Config.get("HttpProxy.Host")
     self.proxyPort = Config.get("HttpProxy.Port")
     self.connection = None
 def __init__(self, hutil):
     try:
         waagent.MyDistro = waagent.GetMyDistro()
         Config = waagent.ConfigurationProvider(None)
     except Exception as e:
         errorMsg = "Failed to construct ConfigurationProvider, which may due to the old wala code with error: %s, stack trace: %s" % (str(e), traceback.format_exc())
         hutil.log(errorMsg)
         Config = waagent.ConfigurationProvider()
     self.logger = hutil
     self.proxyHost = Config.get("HttpProxy.Host")
     self.proxyPort = Config.get("HttpProxy.Port")
     self.tmpFile = './tmp_file_FD76C85E-406F-4CFA-8EB0-CF18B123365C'
def main():
    waagent.LoggerInit('/var/log/waagent.log', '/dev/stdout')
    waagent.Log("%s started to handle." % (ExtensionShortName))
    waagent.MyDistro = waagent.GetMyDistro()

    for a in sys.argv[1:]:
        if re.match("^([-/]*)(disable)", a):
            disable()
        elif re.match("^([-/]*)(uninstall)", a):
            uninstall()
        elif re.match("^([-/]*)(install)", a):
            install()
        elif re.match("^([-/]*)(enable)", a):
            enable()
        elif re.match("^([-/]*)(update)", a):
            update()
def _set_user_account_pub_key(protect_settings, hutil):
    waagent.MyDistro = waagent.GetMyDistro()
    ovf_xml = waagent.GetFileContents('/var/lib/waagent/ovf-env.xml')
    ovf_env = waagent.OvfEnv().Parse(ovf_xml)
    # user name must be provided if set ssh key or password
    if protect_settings and protect_settings.has_key('username'):
        user_name = protect_settings['username']
        user_pass = protect_settings.get('password')
        cert_txt = protect_settings.get('ssh_key')
        if user_pass or cert_txt or len(ovf_env.SshPublicKeys) > 0:
            error_string = waagent.MyDistro.CreateAccount(
                user_name, user_pass, None, None)
            if error_string != None:
                raise Exception(
                    "Failed to create the account or set the password: "******"Succeeded in create the account or set the password.")
            if cert_txt or not (user_pass) and len(ovf_env.SshPublicKeys) > 0:
                pub_path = os.path.join('/home/', user_name, '.ssh',
                                        'authorized_keys')
                ovf_env.UserName = user_name
                if cert_txt:
                    _save_cert_str_as_file(cert_txt, 'temp.crt')
                else:
                    for pkey in ovf_env.SshPublicKeys:
                        if pkey[1]:
                            os.rename(pkey[0] + '.crt',
                                      os.path.join(os.getcwd(), 'temp.crt'))
                            break
                pub_path = ovf_env.PrepareDir(pub_path)
                retcode = waagent.Run(
                    waagent.Openssl +
                    " x509 -in temp.crt -noout -pubkey > temp.pub")
                if retcode > 0:
                    raise Exception("Failed to generate public key file.")
                waagent.MyDistro.setSelinuxContext(
                    'temp.pub', 'unconfined_u:object_r:ssh_home_t:s0')
                waagent.MyDistro.sshDeployPublicKey('temp.pub', pub_path)
                waagent.MyDistro.setSelinuxContext(
                    pub_path, 'unconfined_u:object_r:ssh_home_t:s0')
                waagent.ChangeOwner(pub_path, user_name)
Example #5
0
def main():
    waagent.LoggerInit('/var/log/waagent.log','/dev/stdout')
    waagent.Log("%s started to handle." %(ExtensionShortName))
    waagent.MyDistro = waagent.GetMyDistro()
    global DistroName, DistroVersion
    distro = platform.dist()
    DistroName = distro[0].lower()
    DistroVersion = distro[1]
    for a in sys.argv[1:]:        
        if re.match("^([-/]*)(disable)", a):
            disable()
        elif re.match("^([-/]*)(uninstall)", a):
            uninstall()
        elif re.match("^([-/]*)(install)", a):
            install()
        elif re.match("^([-/]*)(enable)", a):
            enable()
        elif re.match("^([-/]*)(daemon)", a):
            daemon()            
        elif re.match("^([-/]*)(update)", a):
            update()
 def __new__(cls, hutil):
     if(cls.__instance is None):
         hutil.log("Creating HttpUtil")
         cls.__instance = super(HttpUtil, cls).__new__(cls)
         Config = None
         cls.__instance.proxyHost = None
         cls.__instance.proxyPort = None
         try:
             waagent.MyDistro = waagent.GetMyDistro()
             Config = waagent.ConfigurationProvider(None)
         except Exception as e:
             errorMsg = "Failed to construct ConfigurationProvider, which may due to the old wala code with error: %s, stack trace: %s" % (str(e), traceback.format_exc())
             hutil.log(errorMsg)
             Config = None
         cls.__instance.logger = hutil
         if Config != None:
             cls.__instance.proxyHost = Config.get("HttpProxy.Host")
             cls.__instance.proxyPort = Config.get("HttpProxy.Port")
         cls.__instance.tmpFile = './tmp_file_FD76C85E-406F-4CFA-8EB0-CF18B123365C'
     else:
         cls.__instance.logger = hutil
         cls.__instance.logger.log("Returning HttpUtil")
     return cls.__instance
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Requires Python 2.6+
#

import unittest
import env
import vmaccess
import os
from Utils.WAAgentUtil import waagent
import Utils.HandlerUtil as Util

waagent.LoggerInit('/tmp/test.log', '/dev/stdout')
waagent.MyDistro = waagent.GetMyDistro()


class Dummy(object):
    pass


hutil = Dummy()
hutil.log = waagent.Log


class TestCreateNewAccount(unittest.TestCase):
    def test_creat_newuser(self):
        settings = {}
        settings['username'] = '******'
        settings['password'] = '******'