From f62eb6c6d5f8b91dffd4f4d0b6850e99859ddd67 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Wed, 25 May 2016 16:17:48 +0530 Subject: [PATCH] [fix] create user in installer --- playbooks/develop/centos.yml | 12 +-- playbooks/develop/create_user.yml | 22 ++++++ playbooks/develop/includes/setup_bench.yml | 14 ++++ playbooks/develop/includes/setup_mariadb.yml | 14 ++-- playbooks/develop/install.yml | 2 + playbooks/develop/macosx.yml | 6 +- playbooks/develop/ubuntu.yml | 6 +- playbooks/install.py | 76 ++++++++++--------- .../includes/setup_bench_production.yml | 5 +- 9 files changed, 99 insertions(+), 58 deletions(-) create mode 100644 playbooks/develop/create_user.yml diff --git a/playbooks/develop/centos.yml b/playbooks/develop/centos.yml index 91e7fde7..6b2158b6 100644 --- a/playbooks/develop/centos.yml +++ b/playbooks/develop/centos.yml @@ -1,8 +1,8 @@ --- - hosts: localhost vars: - bench_repo_path: "/usr/local/frappe/bench-repo" - bench_path: "/home/{{ ansible_user_id }}/frappe/frappe-bench" + bench_repo_path: "/home/{{ ansible_user_id }}/.bench" + bench_path: "/home/{{ ansible_user_id }}/frappe-bench" mysql_config_template: "templates/simple_mariadb_config.cnf" mysql_conf_dir: /etc/my.cnf.d/ wkhtmltopdf_version: 0.12.2.1 @@ -17,9 +17,6 @@ - name: install prequisites yum: pkg={{ item }} state=present with_items: - # Install epel-release - - epel-release - # basic installs - redis - nodejs @@ -45,9 +42,6 @@ - libtiff-devel - tcl-devel - tk-devel - - # To ensure that ansible_lsb is set - - redhat-lsb-core become: yes become_user: root @@ -65,4 +59,4 @@ # setup development environment - include: includes/setup_dev_env.yml - when: not setup_production + when: not production diff --git a/playbooks/develop/create_user.yml b/playbooks/develop/create_user.yml new file mode 100644 index 00000000..f283779b --- /dev/null +++ b/playbooks/develop/create_user.yml @@ -0,0 +1,22 @@ +--- + - hosts: localhost + tasks: + - name: Create user + user: + name: '{{ frappe_user }}' + generate_ssh_key: yes + + - name: Set home folder perms + file: + path: '/home/{{ frappe_user }}' + mode: 'o+rx' + when: ansible_distribution == 'Ubuntu' or ansible_distribution == 'CentOS' + + - name: Set home folder perms + file: + path: '/Users/{{ frappe_user }}' + mode: 'o+rx' + when: ansible_distribution == 'MacOSX' + + - name: Set /tmp/.bench folder perms + command: 'chown -R {{ frappe_user }}:{{ frappe_user }} /tmp/.bench' diff --git a/playbooks/develop/includes/setup_bench.yml b/playbooks/develop/includes/setup_bench.yml index ee5cc245..f25cf9f0 100644 --- a/playbooks/develop/includes/setup_bench.yml +++ b/playbooks/develop/includes/setup_bench.yml @@ -1,4 +1,18 @@ --- + - name: Check if /tmp/.bench exists + stat: + path: /tmp/.bench + register: tmp_bench + + - name: Check if bench_repo_path exists + stat: + path: '{{ bench_repo_path }}' + register: bench_repo_register + + - name: move /tmp/.bench if it exists + command: 'cp -R /tmp/.bench {{ bench_repo_path }}' + when: tmp_bench.stat.exists and not bench_repo_register.stat.exists + - name: install bench pip: name={{ bench_repo_path }} extra_args='-e' become: yes diff --git a/playbooks/develop/includes/setup_mariadb.yml b/playbooks/develop/includes/setup_mariadb.yml index a6717f5e..207e2fcd 100644 --- a/playbooks/develop/includes/setup_mariadb.yml +++ b/playbooks/develop/includes/setup_mariadb.yml @@ -10,9 +10,11 @@ host: '{{ item }}' password: '{{ mysql_root_password }}' state: present - login_user: root with_items: - localhost + - 127.0.0.1 + - ::1 + become: yes become_user: root # when you have already defined mysql root password @@ -30,11 +32,11 @@ become_user: root when: ansible_os_family == 'RedHat' or ansible_os_family == 'Debian' - - name: Set root password on CentOS - command: mysqladmin -u root password '{{ mysql_root_password }}' - become: yes - become_user: root - when: mysql_root_password is defined and ansible_distribution == 'CentOS' + # - name: Set root password on CentOS + # command: mysqladmin -u root password '{{ mysql_root_password }}' + # become: yes + # become_user: root + # when: mysql_root_password is defined and ansible_distribution == 'CentOS' - name: add launchagents folder mac file: path=~/Library/LaunchAgents state=directory diff --git a/playbooks/develop/install.yml b/playbooks/develop/install.yml index ed7898f4..5c78c508 100644 --- a/playbooks/develop/install.yml +++ b/playbooks/develop/install.yml @@ -3,7 +3,9 @@ - include: macosx.yml when: ansible_distribution == 'MacOSX' + - include: ubuntu.yml when: ansible_distribution == 'Ubuntu' + - include: centos.yml when: ansible_distribution == 'CentOS' diff --git a/playbooks/develop/macosx.yml b/playbooks/develop/macosx.yml index 97174814..0a846562 100644 --- a/playbooks/develop/macosx.yml +++ b/playbooks/develop/macosx.yml @@ -1,8 +1,8 @@ --- - hosts: localhost vars: - bench_repo_path: "/usr/local/frappe/bench-repo" - bench_path: "/Users/{{ ansible_user_id }}/frappe/frappe-bench" + bench_repo_path: "/Users/{{ ansible_user_id }}/.bench" + bench_path: "/Users/{{ ansible_user_id }}/frappe-bench" mysql_config_template: "templates/simple_mariadb_config.cnf" mysql_conf_dir: /usr/local/etc/my.cnf.d @@ -31,4 +31,4 @@ # setup development environment - include: includes/setup_dev_env.yml - when: not setup_production + when: not production diff --git a/playbooks/develop/ubuntu.yml b/playbooks/develop/ubuntu.yml index 6df197c3..20106c2e 100644 --- a/playbooks/develop/ubuntu.yml +++ b/playbooks/develop/ubuntu.yml @@ -1,8 +1,8 @@ --- - hosts: localhost vars: - bench_repo_path: "/usr/local/frappe/bench-repo" - bench_path: "/home/{{ ansible_user_id }}/frappe/frappe-bench" + bench_repo_path: "/home/{{ ansible_user_id }}/.bench" + bench_path: "/home/{{ ansible_user_id }}/frappe-bench" mysql_config_template: "templates/simple_mariadb_config.cnf" mysql_conf_dir: /etc/mysql/conf.d/ wkhtmltopdf_version: 0.12.2.1 @@ -73,4 +73,4 @@ # setup development environment - include: includes/setup_dev_env.yml - when: not setup_production + when: not production diff --git a/playbooks/install.py b/playbooks/install.py index 7ebb5583..4ddf8b51 100644 --- a/playbooks/install.py +++ b/playbooks/install.py @@ -1,12 +1,8 @@ # wget setup_frappe.py | python -import os -import sys -import subprocess -import getpass -import json, multiprocessing +import os, sys, subprocess, getpass, json, multiprocessing, shutil from distutils.spawn import find_executable -bench_repo = '/usr/local/frappe/bench-repo' +tmp_bench_repo = '/tmp/.bench' def install_bench(args): # pre-requisites for bench repo cloning @@ -17,7 +13,7 @@ def install_bench(args): ], 'yum': [ 'sudo yum groupinstall -y "Development tools"', - 'sudo yum install -y git python-setuptools python-devel openssl-devel libffi-devel' + 'sudo yum install -y epel-release redhat-lsb-core git python-setuptools python-devel openssl-devel libffi-devel' ], # epel-release is required to install redis, so installing it before the playbook-run. # redhat-lsb-core is required, so that ansible can set ansible_lsb variable @@ -56,17 +52,26 @@ def install_bench(args): if not success: could_not_install('Ansible') - if is_sudo_user(): - raise Exception('Please run this script as a non-root user with sudo privileges, but without using sudo') - # clone bench repo - clone_bench_repo() + clone_bench_repo(args) + + if is_sudo_user() and not args.user and not args.production: + raise Exception('Please run this script as a non-root user with sudo privileges, but without using sudo or pass --user=USER') + + # create user if not exists + run_playbook('develop/create_user.yml', user=args.user, extra_args=vars(args)) # args is namespace, but we would like to use it as dict in calling function, so use vars() if args.develop: - run_playbook('develop/install.yml', sudo=True, extra_args=vars(args)) - elif args.setup_production: - run_playbook('production/install.yml', sudo=True, extra_args=vars(args)) + run_playbook('develop/install.yml', sudo=True, user=args.user, extra_args=vars(args)) + + elif args.production: + if not args.user: + args.user = 'frappe' + + run_playbook('production/install.yml', sudo=True, user=args.user, extra_args=vars(args)) + + shutil.rmtree(tmp_bench_repo) def install_python27(): version = (sys.version_info[0], sys.version_info[1]) @@ -89,25 +94,16 @@ def install_python27(): # replace current python with python2.7 os.execvp('python2.7', ([] if is_sudo_user() else ['sudo']) + ['python2.7', __file__] + sys.argv[1:]) -def clone_bench_repo(): +def clone_bench_repo(args): '''Clones the bench repository in the user folder''' - - if os.path.exists(bench_repo): + if os.path.exists(tmp_bench_repo): return 0 - run_os_command({ - 'brew': 'mkdir -p /usr/local/frappe', - 'apt-get': 'sudo mkdir -p /usr/local/frappe', - 'yum': 'sudo mkdir -p /usr/local/frappe', - }) - - # change user - run_os_command({ - 'ls': 'sudo chown -R {user}:{user} /usr/local/frappe'.format(user=getpass.getuser()), - }) + branch = args.bench_branch or 'develop' success = run_os_command( - {'git': 'git clone https://github.com/frappe/bench {bench_repo} --depth 1 --branch develop'.format(bench_repo=bench_repo)} + {'git': 'git clone https://github.com/frappe/bench {bench_repo} --depth 1 --branch {branch}'.format( + bench_repo=tmp_bench_repo, branch=branch)} ) return success @@ -167,7 +163,7 @@ def get_passwords(run_travis=False): } def get_extra_vars_json(extra_args, run_travis=False): - # We need to pass setup_production as extra_vars to the playbook to execute conditionals in the + # We need to pass production as extra_vars to the playbook to execute conditionals in the # playbook. Extra variables can passed as json or key=value pair. Here, we will use JSON. json_path = os.path.join(os.path.abspath(os.path.expanduser('~')), 'extra_vars.json') extra_vars = dict(extra_args.items()) @@ -177,7 +173,7 @@ def get_extra_vars_json(extra_args, run_travis=False): extra_vars.update(get_passwords(run_travis)) # Decide for branch to be cloned depending upon whether we setting up production - branch = 'master' if extra_args.get('setup_production') else 'develop' + branch = 'master' if extra_args.get('production') else 'develop' extra_vars.update(branch=branch) # Get max worker_connections in nginx. @@ -190,17 +186,23 @@ def get_extra_vars_json(extra_args, run_travis=False): return ('@' + json_path) -def run_playbook(playbook_name, sudo=False, extra_args=None): +def run_playbook(playbook_name, sudo=False, user=None, extra_args=None): + user = user or getpass.getuser() + if user == 'root': + raise Exception('--user cannot be root') + + extra_args['frappe_user'] = user extra_vars = get_extra_vars_json(extra_args) + args = ['ansible-playbook', '-c', 'local', playbook_name, '-e', extra_vars] if sudo: - args.extend(['--become', '--become-user=frappe']) + args.extend(['--become', '--become-user={0}'.format(user)]) if extra_args.get('verbosity'): args.append('-vvvv') - success = subprocess.check_call(args, cwd=os.path.join(bench_repo, 'playbooks')) + success = subprocess.check_call(args, cwd=os.path.join(tmp_bench_repo, 'playbooks')) return success def parse_commandline_args(): @@ -208,14 +210,14 @@ def parse_commandline_args(): parser = argparse.ArgumentParser(description='Frappe Installer') - # Arguments develop and setup-production are mutually exclusive both can't be specified together. + # Arguments develop and production are mutually exclusive both can't be specified together. # Hence, we need to create a group for discouraging use of both options at the same time. args_group = parser.add_mutually_exclusive_group() args_group.add_argument('--develop', dest='develop', action='store_true', default=False, help='Install developer setup') - args_group.add_argument('--setup-production', dest='setup_production', action='store_true', + args_group.add_argument('--production', dest='production', action='store_true', default=False, help='Setup Production environment for bench') parser.add_argument('--site', dest='site', action='store', default='site1.local', @@ -224,6 +226,10 @@ def parse_commandline_args(): parser.add_argument('--verbose', dest='verbosity', action='store_true', default=False, help='Run the script in verbose mode') + parser.add_argument('--user', dest='user', help='Install frappe-bench for this user') + + parser.add_argument('--bench-branch', dest='bench_branch', help='Clone a particular branch of bench repository') + # To enable testing of script using Travis, this should skip the prompt parser.add_argument('--run-travis', dest='run_travis', action='store_true', default=False, help=argparse.SUPPRESS) diff --git a/playbooks/production/includes/setup_bench_production.yml b/playbooks/production/includes/setup_bench_production.yml index 11428024..6aedbe06 100644 --- a/playbooks/production/includes/setup_bench_production.yml +++ b/playbooks/production/includes/setup_bench_production.yml @@ -1,7 +1,7 @@ --- - hosts: localhost vars: - bench_path: "/home/{{ ansible_user_id }}/frappe/frappe-bench" + bench_path: "/home/{{ ansible_user_id }}/frappe-bench" tasks: @@ -34,11 +34,12 @@ - name: Change permissions for frappe home folder file: - dest: /home/{{ ansible_user_id }} + dest: '{{ ansible_env.HOME }}' owner: '{{ ansible_user_id }}' group: '{{ ansible_user_id }}' mode: 0755 recurse: yes + state: directory - name: Setup production become: yes