Wongzigii Wongzigii - 4 months ago 7x
Bash Question

How to mix several python scripts into a shell script?

I have a local repository and now I would like to deploy it to a docker container. I'd like to write a shell script to handle the setup of environment.

The shell script may do somethings like this in order:

1.Install virtualenv

pip install virtualenv

2.Setup a new virtual environment called

virtualenv new-env

3.Enter the environment

cd new-env


source ./bin/activate

5.Then we need to install dependencies with
that I exported with
pip freeze > requirements.txt
. Notice that I put this file in the root directory.


pip install -r ../requirements.txt

Is it possible to mix these python scripts into one shell script so that I could setup with just a
script? Thanks.


Ansible is a good choice for this. Its easy to get started and can scale up as your system complexity increases. Please refer to this tutorial. A simply yml file to install pip, virtualenv etc. with ansible could look something like:

- name: download pip
  get_url: url=https://bootstrap.pypa.io/get-pip.py dest=/tmp

- name: install pip
  command: "python /tmp/get-pip.py"

- name: Install virtualenv
  pip: name=virtualenv

- name: Create virtualenv path
  file: path={{ virtualenv_path }} state=directory 

- name: Install pip packages under virtualenv
  pip: requirements=/home/user/pip_list.txt virtualenv={{ virtualenv_path }}
  tags: packages

There's also some boilerplate setup to define variables like virtualenv_path & your hosts etc. Just clone a simple repo from github to get started.

Documentation is pretty good for ansible. Just keep googling for your next step and you'll find decent answers here.