Issue
What I normally do is that I open a terminal in ubuntu and type this:
ssh -p 2200 [email protected]
then I enter the password
I then activate my virtual environment:
source /home/myproject/virtualenv/myproject/3.5/bin/activate
then I change the working directory:
cd myproject
I then go to django shell and do some stuff in a script I have written:
python manage.py shell
exec(open('populate_database/populate_database_programs.py').read())
I then exit from django shell
exit()
and exit from ssh
exit
What I want to do is automate all of these.
How can I do all these using Python? I see that there some libraries to connect to ssh but I did not see entering password in non of them
I need to do all these in python numerous time. The reason is that I have a script to populate my database. To insert each record, I open a file and read from the file. There are around 70,000 files. So it takes me around 3 hours and the problem happens in here. My Host Provider disconnects me every 15 minutes. SO I decided to decide the process to a few batches. and Automate all
Solution
If you're just creating data in your database (i.e this will happen only one time), I would advise you to create a data migration using Django's RunPython
. You can loop through all your files and insert the data in your db in one go. Just plug-in your existing python script. Even if the operation takes a long time and you get disconnected, it won't kill the process. Just run it via manage.py migrate &
to make it background process.
If you're dealing with something you want to do regularly, like deploying your website, or some regular clean-up task you need to do, then Fabric is a good way to script your automated tasks over SSH with python. It's typically used to automate deployments on smaller scale systems.
For more complex things, there are frameworks like Ansible, Chef, Puppet. These will also help you manage configurations.
Answered By - dirkgroten