Zigii Wong
Zigii Wong

Reputation: 7826

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 new-env

virtualenv new-env

3.Enter the environment

cd new-env

4.activate

source ./bin/activate

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

So:

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 sh script? Thanks.

Upvotes: 2

Views: 143

Answers (1)

Ken
Ken

Reputation: 795

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.

Upvotes: 1

Related Questions