Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

MongoDB deployment script using PyMongo and Fabric

NotificationsYou must be signed in to change notification settings

ratulbasak/MongoDB-deployment-script-using-PyMongo-and-Fabric

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

We are going to create a fabric file which will deploy the mongodb to the Internet. To do this, we should have to have an idea about fabric.

Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. It provides a basic suite of operations for executing local or remote shell commands (normally or via sudo) and uploading/downloading files, as well as auxiliary functionality such as prompting the running user for input, or aborting execution.

'''Task-1:'''First, clone the repo and you need to backup the db( or download from s3 then unzip db ) and place it to the repo directory where you cloned. As example, in our case "db-folder" is the backed up directory.

'''Task-2:'''Goto the fabric file and take a look at thedev() function. In this function there are some environment variables which defines the database information.

env.mongo_script = 'runDB.py'env.db_path = 'db-folder'env.db_name = 'db-folder'env.db_user = 'db-folder'env.db_pwd = 'your db-folder password'

Here in this snippet, theenv.mongo_script variable contains a script which will create the database and user with username, password and db-roles. Here is the code:

from pymongo import MongoClientclient = MongoClient('localhost:27017')db = client.db-foldercoll = db.datasetdb.add_user('db-folder', 'db-password', roles=["readWrite", "dbAdmin"])

The other environment variables likeenv.db_path,env.db_name,env.db_user andenv.db_pwd contains the value of database path (/home/ubuntu/db-folder), name of database, username and password.

'''Task-3:'''In theinstall() function firstly, the system will be updated then install mongodb using apt sources. In themongodb_config() function, mongodb systemd file path is specified. If the path exists then it'll remove the file and upload mongodb.service systemd file. Here is the snippet:

def mongodb_config():   print 'configuring mongodb'   default_config='/etc/systemd/system/mongodb.service'   if exists(default_config):       sudo('rm /etc/systemd/system/mongodb.service')       print 'Deleted default config'   print 'Install mongodb config service file'   put('%s' % (env.mongodb_service_config), '/etc/systemd/system/', use_sudo=True)

The next task of this function is to specify the mongodb configuration file path. It removes the file if exists and upload the localmongod.conf file from the repo directory. Here is the snippet:

def mongodb_config():  default_config='/etc/mongod.conf'  if exists(default_config):    sudo('rm /etc/mongod.conf')    print 'Deleted default mongo config for login and security'  print 'Install mongodb config file for security and remote login'  put('%s' % (env.mongodb_config), '/etc/', use_sudo=True)  sudo('systemctl daemon-reload')  sudo('systemctl restart mongodb')  return

Now, theinstall_pymongo() function installs pymongo so that therunDB.py can run perfectly on the server. Therunning_db() function first upload the script to the home directory and run the script. Then restore the database usingmongorestore command and enabled the authentication by editing the/etc/mongod.conf file and then restart the mongodb service.

Lastly, thesync_code_base() function upload the database to the server's home directory which is called in thedeploy() function.

About

MongoDB deployment script using PyMongo and Fabric

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp