Beispiel #1
0
def ubuntu_apache_install():
    _logger.info('Installing Apache web server')
    if not tools.ubuntu_install_package(['apache2']):
        _logger.error('Failed to install apache package. Exiting.')
        return False
    _logger.info('Making SSL certificate for Apache')
    if tools.exec_command('make-ssl-cert generate-default-snakeoil',
                          as_root=True):
        _logger.warning('Failed to make SSL certificates.')
        # Snakeoil certificate files:
        # /usr/share/ssl-cert/ssleay.cnf
        # /etc/ssl/certs/ssl-cert-snakeoil.pem
        # /etc/ssl/private/ssl-cert-snakeoil.key
    _logger.info('Enabling Apache modules and sites')
    if tools.exec_command(
            'a2enmod ssl rewrite suexec include proxy proxy_http proxy_connect proxy_ftp headers',
            as_root=True):
        _logger.error('Failed to enable Apache modules. Exiting.')
        return False
    if tools.exec_command('a2ensite 000-default default-ssl', as_root=True):
        _logger.error('Failed to enable Apache sites. Exiting.')
        return False
    if tools.exec_command('service apache2 restart', as_root=True):
        _logger.warning('Failed to restart apache. Exiting.')
        return False
    return True
Beispiel #2
0
 def _ubuntu_do_install_apache(self):
     if not apache.apache_install():
         _logger.error('Failed to install Apache. Exiting.')
         return False
     _logger.info('Configuring site config files.')
     if tools.exec_command(
             'cp %s/odootools/odoo/static/apache/apache-odoo /etc/apache2/sites-available/odoo'
             % config.params['odootools_path'],
             as_root=True):
         _logger.warning('Failed copy Apache odoo site conf file.')
     if tools.exec_command('mkdir -p /etc/odoo/apache2/rewrites',
                           as_root=True):
         _logger.warning('Failed make /etc/odoo/apache2/rewrites.')
     if tools.exec_command(
             'cp %s/odootools/odoo/static/apache/apache-ssl-%s-skeleton /etc/odoo/apache2/ssl-%s-skeleton'
             %
         (config.params['odootools_path'], self._branch, self._branch),
             as_root=True):
         _logger.warning('Failed copy Apache rewrite skeleton.')
     if tools.exec_command(
             'sed -i "s#ServerAdmin .*\\$#ServerAdmin [email protected]\\n\\n\\tInclude /etc/apache2/sites-available/odoo#g" /etc/apache2/sites-available/000-default.conf',
             as_root=True):
         _logger.warning('Failed config Apache site.')
     if tools.exec_command(
             'sed -i "s#ServerAdmin .*\\$#ServerAdmin [email protected]\\n\\n\\tInclude /etc/odoo/apache2/rewrites#g" /etc/apache2/sites-available/default-ssl.conf',
             as_root=True):
         _logger.warning('Failed config Apache site.')
     if not apache.apache_restart():
         _logger.warning('Failed restart Apache.')
     return True
Beispiel #3
0
    def _download_odoo_repo(self):
        if os.path.isdir('/srv/odoo'):
            _logger.warning('/srv/odoo already exists. Not downloading repo.')
            return False

        _logger.info('Creating the directory structure.')
        if tools.exec_command('mkdir -p /srv/odoo/%s/instances' % self._branch,
                              as_root=True):
            _logger.error(
                'Failed to create the odoo directory structure. Exiting.')
            return False
        if tools.exec_command('mkdir -p /srv/odoo/%s/src' % self._branch,
                              as_root=True):
            _logger.error(
                'Failed to create the odoo src directory structure. Exiting.')
            return False
        cwd = os.getcwd()
        os.chdir('/srv/odoo/%s/src/' % self._branch)
        _logger.info(
            'Cloning the latest Odoo git repository from https://github.com/CLEARCORP/odoo.git latest %s branch.'
            % self._branch)
        repo = git_lib.git_clone('https://github.com/CLEARCORP/odoo.git',
                                 'odoo',
                                 branch=self._branch)
        os.chdir(cwd)
        _logger.info('Cloning finished.')
        return True
Beispiel #4
0
def bzr_init_repo(target, no_trees=False):
    #TODO: do this with the python library
    if no_trees:
        if tools.exec_command('bzr init-repo --no-tree %s' % target):
            _logger.error('Failed to create repository in: %s. Exiting.' %
                          target)
            return False
    else:
        if tools.exec_command('bzr init-repo %s' % target):
            _logger.error('Failed to create repository in: %s. Exiting.' %
                          target)
            return False
    return True
Beispiel #5
0
 def _add_postgresql_user(self):
     _logger.info('Adding PostgreSQL user: %s.' % self._user)
     if tools.exec_command(
             'sudo -u postgres createuser %s --superuser --createdb --no-createrole'
             % self._user):
         _logger.error('Failed to add PostgreSQL user. Exiting.')
         return False
     if tools.exec_command(
             'sudo -u postgres psql template1 -U postgres -c "alter user \\"%s\\" with password \'%s\'"'
             % (self._user, self._admin_password)):
         _logger.error('Failed to set PostgreSQL user password. Exiting.')
         return False
     return True
Beispiel #6
0
 def _ubuntu_do_update_postgres_hba(self):
     #TODO: change sed command with python lib to do the change (be carefull with the user perms)
     if tools.exec_command(
             "sed -i 's/\(local[[:space:]]*all[[:space:]]*all[[:space:]]*\)\(ident[[:space:]]*sameuser\)/\1md5/g' /etc/postgresql/%s/main/pg_hba.conf"
             % self._postgresql_version,
             as_root=True):
         _logger.error(
             'Failed to set PostgreSQL pg_hba.conf file. Exiting.')
         return False
     if tools.exec_command('service postgresql restart', as_root=True):
         _logger.error('Failed to start PostgreSQL. Exiting.')
         return False
     return True
Beispiel #7
0
 def _set_logrotation(self):
     if tools.exec_command(
             'cp %s/odootools/odoo/static/log/odoo.logrotate /etc/logrotate.d/'
             % config.params['odootools_path'],
             as_root=True):
         _logger.error('Failed to copy logrotate. Exiting.')
         return False
     return True
Beispiel #8
0
 def _change_postgresql_admin_password(self):
     _logger.info('Changing PostgreSQL admin password.')
     if tools.exec_command(
             'sudo -u postgres psql template1 -U postgres -c "alter user postgres with password \'%s\'"'
             % self._postgresql_password):
         _logger.error('Failed to set PostgreSQL admin password. Exiting.')
         return False
     return True
Beispiel #9
0
    def change_perms(self):
        _logger.debug('User: %s' % self._user)
        if os.path.isdir('/srv/odoo'):
            if tools.exec_command('chown -R %s:odoo /srv/odoo' % self._user,
                                  as_root=True):
                _logger.warning('Failed to set /srv/odoo owner. Skipping.')
            if tools.exec_command('chmod -R g+w /srv/odoo', as_root=True):
                _logger.warning('Failed to set /srv/odoo perms. Skipping.')
        if os.path.isdir('/var/log/odoo'):
            if tools.exec_command('chown -R %s:odoo /var/log/odoo' %
                                  self._user,
                                  as_root=True):
                _logger.warning('Failed to set /var/log/odoo owner. Skipping.')
            if tools.exec_command('chmod -R g+w /var/log/odoo', as_root=True):
                _logger.warning('Failed to set /var/log/odoo perms. Skipping.')
        if os.path.isdir('/var/run/odoo'):
            if tools.exec_command('chown -R %s:odoo /var/run/odoo' %
                                  self._user,
                                  as_root=True):
                _logger.warning('Failed to set /var/run/odoo owner. Skipping.')
            if tools.exec_command('chmod -R g+w /var/run/odoo', as_root=True):
                _logger.warning('Failed to set /var/run/odoo perms. Skipping.')
        if os.path.isdir('/etc/odoo'):
            if tools.exec_command('chown -R %s:odoo /etc/odoo' % self._user,
                                  as_root=True):
                _logger.warning('Failed to set /etc/odoo owner. Skipping.')

        return True
Beispiel #10
0
 def _add_postgresql_user(self):
     _logger.info('Adding PostgreSQL user: odoo_%s.' % self._name)
     if tools.exec_command(
             'adduser --system --home /var/run/odoo/%s --no-create-home --ingroup odoo odoo_%s'
             % (self._name, self._name),
             as_root=True):
         _logger.error('Failed to add system user. Exiting.')
         return False
     if tools.exec_command(
             'sudo -u postgres createuser odoo_%s --superuser --createdb --no-createrole'
             % self._name):
         _logger.error('Failed to add PostgreSQL user. Exiting.')
         return False
     if tools.exec_command(
             'sudo -u postgres psql template1 -U postgres -c "alter user \\"odoo_%s\\" with password \'%s\'"'
             % (self._name, self._admin_password)):
         _logger.error('Failed to set PostgreSQL user password. Exiting.')
         return False
     return True
Beispiel #11
0
    def _add_odoo_user(self):
        try:
            group = grp.getgrnam('odoo')
        except:
            _logger.info('odoo group doesn\'t exist, creating group.')
            # TODO: no addgroup in arch
            tools.exec_command('addgroup odoo', as_root=True)
            group = False
        else:
            _logger.debug('odoo group already exists.')

        try:
            pw = pwd.getpwnam(self._user)
        except:
            _logger.info('Creating user: (%s)' % self._user)
            tools.exec_command(
                'adduser --system --home /var/run/odoo --no-create-home --ingroup odoo %s'
                % self._user,
                as_root=True)
        else:
            _logger.info('User %s already exists, adding to odoo group.' %
                         self._user)
            tools.exec_command('adduser %s odoo' % self._user, as_root=True)

        return True
Beispiel #12
0
 def _arch_do_install_apache(self):
     if not apache.apache_install():
         _logger.error('Failed to install Apache. Exiting.')
         return False
     #TODO: lp:1133385 arch configuration of sites
     _logger.info('Configuring site config files.')
     #if tools.exec_command('cp %s/oerptools/oerp/static/apache/apache-erp /etc/apache2/sites-available/erp' % config.params['oerptools_path'], as_root=True):
     #    _logger.warning('Failed copy Apache erp site conf file.')
     if tools.exec_command('mkdir -p /etc/odoo/apache2/rewrites',
                           as_root=True):
         _logger.warning('Failed make /etc/odoo/apache2/rewrites.')
     if tools.exec_command(
             'cp %s/odootools/odoo/static/apache/apache-ssl-%s-skeleton /etc/odoo/apache2/ssl-%s-skeleton'
             % (config.params['odootools_path'], branch, branch),
             as_root=True):
         _logger.warning('Failed copy Apache rewrite skeleton.')
     #if tools.exec_command('sed -i "s/ServerAdmin .*$/ServerAdmin [email protected]\n\n\tInclude \/etc\/apache2\/sites-available\/erp/g" /etc/apache2/sites-available/default', as_root=True):
     #    _logger.warning('Failed config Apache site.')
     #if tools.exec_command('sed -i "s/ServerAdmin .*$/ServerAdmin [email protected]\n\n\tInclude \/etc\/openerp\/apache2\/rewrites/g" /etc/apache2/sites-available/default-ssl', as_root=True):
     #    _logger.warning('Failed config Apache site.')
     if apache.apache_restart():
         _logger.warning('Failed restart Apache.')
     return True
Beispiel #13
0
    def _arch_install_postgresql(self):
        #TODO: change some of this calls with a python way to do it (because of perms)
        if os.path.isdir('/var/lib/postgres/data'):
            _logger.info(
                'PostgreSQL appears to be already configured. Skipping.')
            return False

        if tools.arch_install_package(['postgresql']):
            _logger.error('Failed to install PostgreSQL. Exiting.')
            return False
        if tools.exec_command('systemd-tmpfiles --create postgresql.conf',
                              as_root=True):
            _logger.error(
                'Failed to configure PostgreSQL systemd script. Exiting.')
            return False
        if tools.exec_command('mkdir /var/lib/postgres/data', as_root=True):
            _logger.error(
                'Failed to create PostgreSQL data directory. Exiting.')
            return False
        if tools.exec_command(
                'chown -c postgres:postgres /var/lib/postgres/data',
                as_root=True):
            _logger.error(
                'Failed to set PostgreSQL data directory permisions. Exiting.')
            return False
        if tools.exec_command(
                'sudo -i -u postgres initdb -D \'/var/lib/postgres/data\''):
            _logger.error('Failed to init PostgreSQL database. Exiting.')
            return False
        if tools.exec_command('systemctl enable postgresql', as_root=True):
            _logger.error('Failed to enable PostgreSQL init script. Exiting.')
            return False
        if tools.exec_command('systemctl start postgresql', as_root=True):
            _logger.error('Failed to start PostgreSQL. Exiting.')
            return False
        return True
Beispiel #14
0
def arch_apache_restart():
    _logger.info('Restarting Apache web server')
    if tools.exec_command('systemctl restart httpd', as_root=True):
        _logger.error('Failed to restart Apache. Exiting.')
        return False
    return True
Beispiel #15
0
    def update_config_file_values(self, values, update_file=None):
        """Update the values in the specified config file.
        
        :params
        
        values dict: one key per section, each section is a dict.
        
        file str: Config file to update.
        """
        import copy, re
        copy_values = copy.deepcopy(values)

        if update_file:
            file_path = update_file
        else:
            if not self.config_files:
                _logger.warning('Not saving values, config file unknown.')
                return False
            else:
                file_path = self.config_files[-1]

        chmod_r = False
        try:
            config_file = open(file_path)
        except:
            try:
                import odootools.lib.tools as tools
                tools.exec_command('chmod o+r %s' % file_path, as_root=True)
                chmod_r = True
                config_file = open(file_path)
            except:
                _logger.error('Failed to open config file for reading: %s' %
                              file_path)
                return False

        new_file = ""
        section = False
        for line in config_file:
            _logger.debug('line: %s' % line)
            #Case: new section
            if re.match(r"^\[[^\s]*\]$", line):
                _logger.debug('New section: %s' % line[1:len(line) - 2])
                #Check to see if there is still values to update in last section
                if section and section in copy_values and copy_values[section]:
                    new_file += '\n#THE FOLLOWING VALUES WHERE AUTOMATICALLY ADDED\n'
                    for key, value in copy_values[section].items():
                        new_file += key + ' = ' + str(value) + '\n'
                    new_file += '\n\n'
                    copy_values.pop(section)
                #Check if section present but empty
                elif section and section in copy_values:
                    copy_values.pop(section)
                new_file += line
                section = line[1:len(line) - 2]
            #Case: no new section, no actual section
            elif not section:
                new_file += line
            #Case: actual section in copy_values
            elif section and section in copy_values:
                #Case: value line
                value_match = re.match(r"^[\s]*([^\s=:;#]+)", line)
                commented_value_match = re.match(
                    r"^[\s]*[#;]+[\s]*([^\s=:;#]+)", line)
                if value_match:
                    #Case: line value is in copy_values to update
                    if value_match.group(1).replace(
                            '-', '_') in copy_values[section]:
                        new_file += value_match.group(1) + ' = ' + str(
                            copy_values[section].pop(
                                value_match.group(1))) + '\n'
                    else:
                        new_file += line
                #Case: commented value line
                elif commented_value_match:
                    #Case: line value is in copy_values to update
                    if commented_value_match.group(1).replace(
                            '-', '_') in copy_values[section]:
                        new_file += commented_value_match.group(
                            1) + ' = ' + str(copy_values[section].pop(
                                commented_value_match.group(1))) + '\n'
                    else:
                        new_file += line
                #Case: other line, comments
                else:
                    new_file += line
            #Case: no actual section, no new section
            else:
                new_file += line

        #Check to see if all copy_values where updated
        if copy_values:
            _logger.debug('Some values remaining, last section: %s' % section)
            for section_key, section_value in copy_values.items():
                if section_value:
                    new_file += '\n#THE FOLLOWING SECTION WAS AUTOMATICALLY ADDED\n'
                    if not section_key == section:
                        new_file += "\n[" + section_key + "]\n"
                    for key, value in section_value.items():
                        new_file += key + " = " + str(value) + '\n'
                    new_file += '\n\n'
                copy_values.pop(section_key)

        config_file.close()

        chmod_w = False
        try:
            config_file = open(file_path, 'w')
            config_file.write(new_file)
            config_file.close()
        except:
            try:
                import odootools.lib.tools as tools
                tools.exec_command('chmod o+w %s' % file_path, as_root=True)
                chmod_w = True
                config_file = open(file_path, 'w')
                config_file.write(new_file)
                config_file.close()
            except:
                _logger.error('Failed to open config file for updating: %s' %
                              file_path)
                return False

        #Check if chmod applied in order to restore perms
        if chmod_r == True:
            tools.exec_command('chmod o-r %s' % file_path, as_root=True)
        if chmod_w == True:
            tools.exec_command('chmod o-w %s' % file_path, as_root=True)

        return file_path
Beispiel #16
0
def ubuntu_apache_restart():
    _logger.info('Restarting Apache web server')
    if tools.exec_command('service apache2 restart', as_root=True):
        _logger.error('Failed to restart Apache. Exiting.')
        return False
    return True
Beispiel #17
0
    def _config_odoo_version(self):
        _logger.info('Configuring Odoo %s' % self._branch)
        cwd = os.getcwd()

        # Odoo Server bin
        _logger.debug('Copy bin script skeleton to /etc')
        if tools.exec_command('mkdir -p /etc/odoo/%s/server/' % self._branch,
                              as_root=True):
            _logger.error(
                'Failed to make /etc/odoo/%s/server/ directory. Exiting.' %
                self._branch)
            return False
        if tools.exec_command(
                'cp %s/odootools/odoo/static/bin/server-bin-%s-skeleton /etc/odoo/%s/server/bin-skeleton'
                %
            (config.params['odootools_path'], self._branch, self._branch),
                as_root=True):
            _logger.error('Failed to copy bin skeleton. Exiting.')
            return False
        if tools.exec_command(
                'sed -i "s#@BRANCH@#%s#g" /etc/odoo/%s/server/bin-skeleton' %
            (self._branch, self._branch),
                as_root=True):  # TODO: check sed command
            _logger.error('Failed to config bin skeleton. Exiting.')
            return False

        # Odoo Server init
        if tools.exec_command(
                'cp %s/odootools/odoo/static/init/server-init-%s-skeleton /etc/odoo/%s/server/init-skeleton'
                %
            (config.params['odootools_path'], self._branch, self._branch),
                as_root=True):
            _logger.error('Failed to copy init skeleton. Exiting.')
            return False
        if tools.exec_command(
                'sed -i "s#@PATH@#/usr/local#g" /etc/odoo/%s/server/init-skeleton'
                % self._branch,
                as_root=True):
            _logger.error('Failed to config init skeleton. Exiting.')
            return False
        if tools.exec_command(
                'sed -i "s#@BRANCH@#%s#g" /etc/odoo/%s/server/init-skeleton' %
            (self._branch, self._branch),
                as_root=True):
            _logger.error('Failed to config init skeleton. Exiting.')
            return False
        if self._installation_type == 'dev':
            if tools.exec_command(
                    'sed -i "s#@USER@#%s#g" /etc/odoo/%s/server/init-skeleton'
                    % (self._user, self._branch),
                    as_root=True):
                _logger.error('Failed to config init skeleton. Exiting.')
                return False
            if tools.exec_command(
                    'sed -i "s#@DBFILTER@#\${SERVERNAME}_.*#g" /etc/odoo/%s/server/init-skeleton'
                    % self._branch,
                    as_root=True):
                _logger.error('Failed to config init skeleton. Exiting.')
                return False
        else:
            if tools.exec_command(
                    'sed -i "s#@DBFILTER@#.*#g" /etc/odoo/%s/server/init-skeleton'
                    % self._branch,
                    as_root=True):
                _logger.error('Failed to config init skeleton. Exiting.')
                return False
        # Odoo Server config
        if tools.exec_command(
                'cp %s/odootools/odoo/static/conf/server.conf-%s-skeleton /etc/odoo/%s/server/conf-skeleton'
                %
            (config.params['odootools_path'], self._branch, self._branch),
                as_root=True):
            _logger.error('Failed to copy conf skeleton. Exiting.')
            return False
        if tools.exec_command(
                'sed -i "s#@BRANCH@#%s#g" /etc/odoo/%s/server/conf-skeleton' %
            (self._branch, self._branch),
                as_root=True):
            _logger.error('Failed to config init skeleton. Exiting.')
            return False
        if self._installation_type == 'dev':
            if tools.exec_command(
                    'sed -i "s#@INTERFACE@##g" /etc/odoo/%s/server/conf-skeleton'
                    % self._branch,
                    as_root=True):
                _logger.error(
                    'Failed to set interface in config skeleton. Exiting.')
                return False
        else:
            if tools.exec_command(
                    'sed -i "s#@INTERFACE@#localhost#g" /etc/odoo/%s/server/conf-skeleton'
                    % self._branch,
                    as_root=True):
                _logger.error(
                    'Failed to set interface in config skeleton. Exiting.')
                return False

        if tools.exec_command('mkdir -p /var/run/odoo', as_root=True):
            _logger.error('Failed to create /var/run/odoo. Exiting.')
            return False

        return True
Beispiel #18
0
    def install(self):
        _logger.info('Odoo instance installation started.')

        _logger.info('')
        _logger.info(
            'Please check the following information before continuing.')
        _logger.info(
            '=========================================================')

        if not self._user:
            if self._installation_type == 'dev':
                self._user = pwd.getpwuid(os.getuid()).pw_name
            elif self._installation_type == 'server':
                self._user = '******'
        if pwd.getpwuid(os.getuid()).pw_name not in (self._user, 'root'):
            try:
                group = grp.getgrnam('odoo')
                if not pwd.getpwuid(os.getuid()).pw_name in group['gr_mem']:
                    _logger.error(
                        'Your user must be the user of installation (%s), root, or be part of odoo group. Exiting.'
                    )
                    return False
            except:
                _logger.error(
                    'Your user must be the user of installation (%s), root, or be part of odoo group. Exiting.'
                )
                return False
        _logger.info('Selected user: %s' % self._user)

        installed_branches = []
        if os.path.isdir('/etc/odoo/7.0'):
            installed_branches.append('7.0')
        if os.path.isdir('/etc/odoo/8.0'):
            installed_branches.append('8.0')
        if os.path.isdir('/etc/odoo/trunk'):
            installed_branches.append('trunk')

        if not installed_branches:
            _logger.error('No Odoo server versions installed. Exiting.')
            return False
        elif len(installed_branches) == 1:
            if self._branch not in installed_branches:
                _logger.warning(
                    'Selected branch (%s) not installed. Using %s instead.' %
                    (self._branch, installed_branches[0]))
                self._branch = installed_branches[0]
        else:
            if self._branch not in installed_branches:
                _logger.error('Selected branch (%s) not installed. Exiting.' %
                              self._branch)
                return False

        _logger.info('Odoo version (branch) used by this instance: %s' %
                     self._branch)

        check_port = self._check_port()
        if re.match(r'[0-9a-z_]+', self._name):
            check_name = self._check_name()
            if check_name:
                _logger.error(
                    'Selected name (%s) is in use by another instance version %s. Exiting.'
                    % (self._name, check_port[0]))
                return False
            else:
                _logger.info('Instance name: %s' % self._name)
        else:
            _logger.error(
                'Selected name (%s) has invalid characters. Use only lowercase letters, digits and underscore. Exiting.'
                % self._name)
            return False

        if os.path.exists('/srv/odoo/%s/instances/%s' %
                          (self._branch, self._name)):
            _logger.error('/srv/odoo/%s/instances/%s already exists. Exiting' %
                          (self._branch, self._name))
            return False

        if not isinstance(self._port, int):
            _logger.error('Instance port unknown. Exiting.')
            return False
        elif self._port >= 0 and self._port <= 99:
            if check_port:
                _logger.error(
                    'Selected port (%02d) is in use by instance %s (%s). Exiting.'
                    % (self._port, check_port[1], check_port[0]))
                return False
            else:
                _logger.info('Instance port number: %02d' % self._port)
        else:
            _logger.error(
                'Selected port (%02d) is invalid. Port number must be an integer number between 0 and 99. Exiting.'
                % self._port)
            return False

        if self._start_now:
            _logger.info(
                'Instance will be started at the end of this installation.')
        else:
            _logger.info(
                'Instance will NOT be started at the end of this installation.'
            )

        if self._on_boot:
            _logger.info('Instance will be started on boot.')
        else:
            _logger.info('Instance will NOT be started on boot.')

        _logger.info('')
        _logger.info('Please review the values above and confirm accordingly.')
        answer = False
        while not answer:
            answer = raw_input('Are the configuration values correct (y/n)? ')
            if re.match(r'^y$|^yes$', answer, flags=re.IGNORECASE):
                answer = 'y'
            elif re.match(r'^n$|^no$', answer, flags=re.IGNORECASE):
                answer = 'n'
                _logger.error(
                    'The configuration values are incorrect. Please correct any configuration error and run the script again.'
                )
                return False
            else:
                answer = False

        #Update config file with new values
        values = {
            'odoo-instance-make': {
                self._branch + '_' + self._name + '_installation_type':
                self._installation_type,
                self._branch + '_' + self._name + '_user':
                self._user,
                self._branch + '_' + self._name + '_port':
                self._port,
                self._branch + '_' + self._name + '_install_odoo_clearcorp':
                self._install_odoo_clearcorp,
                self._branch + '_' + self._name + '_install_odoo_costa_rica':
                self._install_odoo_costa_rica,
                self._branch + '_' + self._name + '_admin_password':
                self._admin_password,
                self._branch + '_' + self._name + '_postgresql_password':
                self._postgresql_password,
                self._branch + '_' + self._name + '_start_now':
                self._start_now,
                self._branch + '_' + self._name + '_on_boot':
                self._on_boot,
            },
        }

        config_file_path = config.params.update_config_file_values(values)
        if config_file_path:
            _logger.info('Updated config file with installation values: %s' %
                         config_file_path)
        else:
            _logger.warning(
                'Failed to update config file with installation values.')

        _logger.info('')
        _logger.info('Installing Odoo instance')
        _logger.info('===========================')
        _logger.info('')

        self._add_postgresql_user()

        os.makedirs('/srv/odoo/%s/instances/%s/addons' %
                    (self._branch, self._name))
        os.makedirs('/srv/odoo/%s/instances/%s/filestore' %
                    (self._branch, self._name))
        os.symlink(
            '/srv/odoo/%s/src/odoo' % self._branch,
            '/srv/odoo/%s/instances/%s/server' % (self._branch, self._name))

        installed_addons = [
            '/srv/odoo/%s/instances/%s/server/addons' %
            (self._branch, self._name)
        ]
        for path in os.listdir('/srv/odoo/%s/src' % self._branch):
            if path not in ('odoo'):
                os.symlink(
                    '/srv/odoo/%s/src/%s' % (self._branch, path),
                    '/srv/odoo/%s/instances/%s/addons/%s' %
                    (self._branch, self._name, path))
                installed_addons.append('/srv/odoo/%s/instances/%s/addons/%s' %
                                        (self._branch, self._name, path))
        installed_addons = ','.join(installed_addons)

        # TODO: lp:1133399 archlinux init
        if tools.exec_command(
                'cp -a /etc/odoo/%s/server/init-skeleton /etc/init.d/odoo-server-%s'
                % (self._branch, self._name),
                as_root=True):
            _logger.warning('Failed to copy init script.')
        else:
            tools.exec_command(
                'sed -i "s#@NAME@#%s#g" /etc/init.d/odoo-server-%s' %
                (self._name, self._name),
                as_root=True)
            tools.exec_command(
                'sed -i "s#@USER@#odoo_%s#g" /etc/init.d/odoo-server-%s' %
                (self._name, self._name),
                as_root=True)
            if self._on_boot:
                tools.exec_command('update-rc.d odoo-server-%s defaults' %
                                   self._name,
                                   as_root=True)

        if tools.exec_command(
                'cp -a /etc/odoo/%s/server/bin-skeleton /usr/local/bin/odoo-server-%s'
                % (self._branch, self._name),
                as_root=True):
            _logger.warning('Failed to copy bin script.')
        else:
            tools.exec_command(
                'sed -i "s#@NAME@#%s#g" /usr/local/bin/odoo-server-%s' %
                (self._name, self._name),
                as_root=True)

        if tools.exec_command(
                'cp -a /etc/odoo/%s/server/conf-skeleton /etc/odoo/%s/server/%s.conf'
                % (self._branch, self._branch, self._name),
                as_root=True):
            _logger.warning('Failed to copy conf file.')
        else:
            tools.exec_command(
                'sed -i "s#@NAME@#%s#g" /etc/odoo/%s/server/%s.conf' %
                (self._name, self._branch, self._name),
                as_root=True)
            tools.exec_command(
                'sed -i "s#@PORT@#%02d#g" /etc/odoo/%s/server/%s.conf' %
                (self._port, self._branch, self._name),
                as_root=True)
            tools.exec_command(
                'sed -i "s#@DB_USER@#odoo_%s#g" /etc/odoo/%s/server/%s.conf' %
                (self._name, self._branch, self._name),
                as_root=True)
            tools.exec_command(
                'sed -i "s#@XMLPORT@#20%02d#g" /etc/odoo/%s/server/%s.conf' %
                (self._port, self._branch, self._name),
                as_root=True)
            tools.exec_command(
                'sed -i "s#@NETPORT@#21%02d#g" /etc/odoo/%s/server/%s.conf' %
                (self._port, self._branch, self._name),
                as_root=True)
            tools.exec_command(
                'sed -i "s#@XMLSPORT@#22%02d#g" /etc/odoo/%s/server/%s.conf' %
                (self._port, self._branch, self._name),
                as_root=True)
            tools.exec_command(
                'sed -i "s#@PYROPORT@#24%02d#g" /etc/odoo/%s/server/%s.conf' %
                (self._port, self._branch, self._name),
                as_root=True)
            tools.exec_command(
                'sed -i "s#@ADMIN_PASSWD@#%s#g" /etc/odoo/%s/server/%s.conf' %
                (self._admin_password, self._branch, self._name),
                as_root=True)
            tools.exec_command(
                'sed -i "s#@ADDONS@#%s#g" /etc/odoo/%s/server/%s.conf' %
                (installed_addons, self._branch, self._name),
                as_root=True)

        if tools.exec_command('mkdir -p /var/log/odoo/%s' % self._name,
                              as_root=True):
            _logger.warning('Failed to make log dir.')
        else:
            tools.exec_command('touch /var/log/odoo/%s/server.log' %
                               self._name,
                               as_root=True)
            tools.exec_command('chown -R odoo_%s:odoo /var/log/odoo/%s' %
                               (self._name, self._name),
                               as_root=True)
            tools.exec_command('chmod 664 /var/log/odoo/%s/*.log' % self._name,
                               as_root=True)

        #TODO: lp:1133403 archlinux apache configurations
        if tools.exec_command(
                'cp -a /etc/odoo/apache2/ssl-%s-skeleton /etc/odoo/apache2/rewrites/%s'
                % (self._branch, self._name),
                as_root=True):
            _logger.warning('Failed copy apache rewrite file.')
        else:
            tools.exec_command(
                'sed -i "s#@NAME@#%s#g" /etc/odoo/apache2/rewrites/%s' %
                (self._name, self._name),
                as_root=True)
            tools.exec_command(
                'sed -i "s#@PORT@#20%02d#g" /etc/odoo/apache2/rewrites/%s' %
                (self._port, self._name),
                as_root=True)
            tools.exec_command('service apache2 reload', as_root=True)

        if tools.exec_command('mkdir -p /var/run/odoo/%s' % self._name,
                              as_root=True):
            _logger.warning('Failed to make pid dir.')

        #TODO: lp:1133399 archlinux init server
        if self._start_now:
            tools.exec_command('service postgresql start', as_root=True)
            tools.exec_command('service apache2 restart', as_root=True)
            tools.exec_command('service odoo-server-%s start' % self._name,
                               as_root=True)

        if self._installation_type == 'dev':
            tools.exec_command(
                'bash -c "echo \\"127.0.1.1    %s.localhost\\" >> /etc/hosts"'
                % self._name,
                as_root=True)

        self._server.change_perms()

        return True
Beispiel #19
0
def load_info():
    res = {}

    # Format for both file and stout logging
    res['file_log_format'] = '%(asctime)s %(levelname)s %(name)s: %(message)s'
    res['stdout_log_format'] = '%(levelname)s %(name)s: %(message)s'

    res['file_handler'] = False
    res['stdout_handler'] = logging.StreamHandler()

    # Set log file handler if needed
    if 'log_file' in config.params and config.params['log_file']:
        # LogFile Handler
        log_file = config.params['log_file']
        dirname = os.path.dirname(log_file)
        dir_exists = True
        if dirname and not os.path.isdir(dirname):
            dir_exists = False
            try:
                os.makedirs(dirname)
                dir_exists = True
            except:
                try:
                    tools.exec_command('mkdir %s' % dirname, as_root=True)
                    tools.exec_command('chmod a+rwx %s' % dirname,
                                       as_root=True)
                    dir_exists = True
                except:
                    _logger.error(
                        "Couldn't create the log file directory. Logging to the standard output."
                    )

        if dir_exists:
            try:
                res['file_handler'] = logging.handlers.WatchedFileHandler(
                    log_file)
            except:
                if dirname:
                    try:
                        tools.exec_command('chmod a+rwx %s' % dirname,
                                           as_root=True)
                        if os.path.isfile(log_file):
                            tools.exec_command('chmod a+rw %s' % log_file,
                                               as_root=True)
                        res['file_handler'] = logging.handlers.WatchedFileHandler(
                            log_file)
                    except:
                        _logger.error(
                            "Couldn't create the log file. Logging to the standard output."
                        )
                else:
                    _logger.error(
                        "Couldn't create the log file. Logging to the standard output."
                    )

    # Set formatters
    if os.isatty(res['stdout_handler'].stream.fileno()):
        res['stdout_handler'].setFormatter(
            ColoredFormatter(res['stdout_log_format']))
    else:
        res['stdout_handler'].setFormatter(
            logging.Formatter(res['stdout_log_format']))
    if res['file_handler']:
        res['file_handler'].setFormatter(
            logging.Formatter(res['file_log_format']))

    # Configure levels
    default_log_levels = [
        ':INFO',
    ]

    # Initialize pseudo log levels for easy log-level config param
    pseudo_log_levels = []
    if 'log_level' in config.params and config.params['log_level']:
        log_level = config.params['log_level']
        if log_level.lower() == 'debug':
            pseudo_log_levels = ['odootools:DEBUG']
        elif log_level.lower() == 'info':
            pseudo_log_levels = ['odootools:INFO']
        elif log_level.lower() == 'warning':
            pseudo_log_levels = ['odootools:WARNING']
        elif log_level.lower() == 'error':
            pseudo_log_levels = ['odootools:ERROR']
        elif log_level.lower() == 'critical':
            pseudo_log_levels = ['odootools:CRITICAL']

    if 'log_handler' in config.params and config.params['log_handler']:
        log_handler = config.params['log_handler']
    else:
        log_handler = []

    res['log_hander_list'] = default_log_levels + pseudo_log_levels + log_handler
    return res