Skip to content

Commit

Permalink
upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
netkiller committed Jun 4, 2014
1 parent d2e33c3 commit 22ba053
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 94 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ nosetests.xml
.pydevproject
/shell/*.bak
MANIFEST
MANIFEST.in
106 changes: 24 additions & 82 deletions bin/deployment
Original file line number Diff line number Diff line change
Expand Up @@ -14,73 +14,16 @@ from datetime import datetime
basedir=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

try:
sys.path.append(basedir + '/lib/python3.3/site-packages')
sys.path.append(basedir + '/lib/python3.4/site-packages')
from rsync import *
import getpass
from git import *
#import getpass
except ImportError as err:
print("Error: %s" %(err))

CONFIG_DIR= basedir + '/etc'
PROJECT_DIR=basedir + '/project'
##############################################
class Git():
cmd = []
def __init__(self, workspace = None, logging = None):
self.logging = logging
self.workspace = workspace
def option(self, opt):
if opt:
self.opt = opt
def clone(self, uri):
if self.workspace :
self.cmd.append('clone '+ uri +' '+ self.workspace)
return(self)
def pull(self):
if self.workspace :
os.chdir(self.workspace)
self.cmd.append('pull --progress')
return(self)
def reset(self):
self.cmd.append('reset HEAD --hard')
return(self)
def branch(self, branchname=None, op=None):
os.chdir(self.workspace)
if branchname :
if op == 'delete':
self.cmd.append('branch -D '+branchname)
elif op == 'new':
self.cmd.append('checkout -fb '+branchname+' --')
else:
self.cmd.append('reset HEAD --hard')
self.cmd.append('fetch origin')
self.cmd.append('checkout -f '+branchname)
else:
self.cmd.append('branch')
return(self)
def merge(self, branchname):
self.cmd.append('merge '+branchname)
return(self)
def tag(self, tagname):
os.chdir(self.workspace)
self.cmd.append('tag ' + tagname)
return(self)
def checkout(self, revision=None):
os.chdir(self.workspace)
if revision :
self.cmd.append('checkout -f '+revision)
return(self)
def debug(self):
cmd = ''
for line in self.cmd:
cmd += 'git ' + line + '; '
return(cmd)
def execute(self):
for line in self.cmd:
os.system('git '+ line)
self.logging.debug('git '+ line)
self.cmd = []
print("-")

#PROJECT_DIR = os.path.expanduser('~/project')
##############################################
class Deployment():
debug = False
def __init__(self):
Expand Down Expand Up @@ -117,9 +60,8 @@ class Deployment():
#group.add_option('','--testcase', help='logs file.', default='backup.log')
self.parser.add_option_group(group)

self.config = self.configure(CONFIG_DIR+'/deployment.cfg','main')
self.scm = self.configure(CONFIG_DIR+'/deployment.cfg','scm')
self.node = self.configure(CONFIG_DIR+'/deployment.cfg','node')
self.config = self.configure(CONFIG_DIR+'/deployment.cfg','scm')
self.project = self.configure(CONFIG_DIR+'/deployment.cfg','project')
self.logging = self.logfile(self.config['logdir']+'/'+self.config['logfile'])

def configure(self,inifile, section = None):
Expand Down Expand Up @@ -173,7 +115,7 @@ class Deployment():

def buildconfig(self, stage, domain, source):
try:
inifile = PROJECT_DIR+'/config/'+stage+'/'+domain+'.ini'
inifile = os.path.expanduser(self.project['cfgdir']+'/config/'+stage+'/'+domain+'.ini')
if os.path.exists(inifile):
sections = self.configure(inifile)
#print(sections)
Expand All @@ -200,12 +142,12 @@ class Deployment():
def deploy(self, stage, domain):
self.logging.info('deployment '+ self.stage+' '+self.domain)
try:
inifile = PROJECT_DIR+'/'+stage+'/'+domain[domain.find('.')+1:]+'.ini'
inifile = os.path.expanduser(self.project['cfgdir']+'/'+stage+'/'+domain[domain.find('.')+1:]+'.ini')
host = domain[:domain.find('.')]
conf = self.configure(inifile, host)

if ('source' not in conf):
source = self.node['source'] +'/'+ stage +'/'+ domain
source = self.project['source'] +'/'+ stage +'/'+ domain
else:
source = conf['source']

Expand Down Expand Up @@ -250,9 +192,9 @@ class Deployment():
rsync = Rsync()

if('option' in conf):
rsync.option('-auzv --exclude=.git --exclude=.svn ' + conf['option'])
rsync.option('-auzv --exclude=.git --exclude=.svn --exclude=.gitignore' + conf['option'])
else:
rsync.option('-auzv --exclude=.git --exclude=.svn')
rsync.option('-auzv --exclude=.git --exclude=.svn --exclude=.gitignore')

if('delete' in conf):
if conf['delete'].lower() in ("yes", "true", "y", "enable") :
Expand All @@ -262,15 +204,15 @@ class Deployment():
if conf['logfile'] :
rsync.logfile(conf['logfile'])
else:
logdir = self.node['logdir']+'/'+stage
logdir = os.path.expanduser(self.project['logdir']+'/'+stage)
if not os.path.exists(logdir):
os.makedirs(logdir)
conf['logfile'] = logdir+'/'+domain+'.'+datetime.today().strftime('%Y-%m-%d')+'.log'
#.%H:%M:%S
rsync.logfile(conf['logfile'])

if('password' in conf):
rsync.password(conf['password'])
rsync.password(os.path.expanduser(conf['password']))

if('backup' in conf):
if conf['backup'] :
Expand All @@ -280,7 +222,7 @@ class Deployment():
rsync.backup(conf['backup'])

if('exclude' in conf):
rsync.exclude(PROJECT_DIR + '/exclude/' + conf['exclude'])
rsync.exclude(os.path.expanduser(self.project['cfgdir'] + '/exclude/' + conf['exclude']))

rsync.source(source+'/')

Expand Down Expand Up @@ -308,12 +250,12 @@ class Deployment():
stage = args[1]
domain =args[2]
try:
inifile = PROJECT_DIR+'/'+stage+'/'+domain[domain.find('.')+1:]+'.ini'
inifile = os.path.expanduser(self.project['cfgdir']+'/'+stage+'/'+domain[domain.find('.')+1:]+'.ini')
host = domain[:domain.find('.')]
conf = self.configure(inifile, host)

if ('source' not in conf):
source = self.node['source'] +'/'+ stage +'/'+ domain
source = self.project['source'] +'/'+ stage +'/'+ domain
else:
source = conf['source']

Expand Down Expand Up @@ -350,10 +292,10 @@ class Deployment():
domain =args[2]

host = domain[:domain.find('.')]
conf = self.configure(PROJECT_DIR+'/'+stage+'/'+domain[domain.find('.')+1:]+'.ini' , host)
conf = self.configure(os.path.expanduser(self.project['cfgdir']+'/'+stage+'/'+domain[domain.find('.')+1:]+'.ini' , host))

workspace = os.path.expanduser('~/.workspace')
#self.scm

project = workspace +'/'+ domain
git = Git(project, self.logging)

Expand Down Expand Up @@ -405,9 +347,9 @@ class Deployment():
try:
config = configparser.SafeConfigParser()
#if domain == None:
for file in os.listdir(PROJECT_DIR+'/'+stage):
for file in os.listdir(os.path.expanduser(self.project['cfgdir']+'/'+stage)):
if file.endswith(".ini"):
config.read(PROJECT_DIR+'/'+stage+'/'+file)
config.read(os.path.expanduser(self.project['cfgdir']+'/'+stage+'/'+file))
print(file)
#for project in config.sections():
# print(project, end = ', ');
Expand Down Expand Up @@ -438,9 +380,9 @@ class Deployment():
if args[1] in ('development','testing','production'):
self.merge(args, self.options.to, self.options.froms)
elif args[0] in ('development','testing','production'):
if self.scm[(args[0])] != getpass.getuser():
print("Current user "+os.getlogin()+" : Permission denied")
sys.exit(127)
#if self.scm[(args[0])] != getpass.getuser():
# print("Current user "+os.getlogin()+" : Permission denied")
# sys.exit(127)
if args.__len__() == 2:
self.stage = args[0]
self.domain =args[1]
Expand Down
8 changes: 4 additions & 4 deletions etc/deployment.cfg
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
[main]
[scm]
;basedir=/usr/local
cfgdir=/srv/devops/etc
logdir=/srv/devops/log
logfile=deployment.log
pidfile=/tmp/oscm.pid

[scm]
;Software Configuration Management Engineer
moderator=neo
development=dev
testing=testing
production=neo

[node]
logdir=/srv/devops/log
[project]
cfgdir=~
logdir=~/log
source=~/.repositories
backup=~/.backup
59 changes: 59 additions & 0 deletions library/git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#-*- coding: utf-8 -*-
import os, sys
class Git():
cmd = []
def __init__(self, workspace = None, logging = None):
self.logging = logging
self.workspace = workspace
def option(self, opt):
if opt:
self.opt = opt
def clone(self, uri):
if self.workspace :
self.cmd.append('clone '+ uri +' '+ self.workspace)
return(self)
def pull(self):
if self.workspace :
os.chdir(self.workspace)
self.cmd.append('pull --progress')
return(self)
def reset(self):
self.cmd.append('reset HEAD --hard')
return(self)
def branch(self, branchname=None, op=None):
os.chdir(self.workspace)
if branchname :
if op == 'delete':
self.cmd.append('branch -D '+branchname)
elif op == 'new':
self.cmd.append('checkout -fb '+branchname+' --')
else:
self.cmd.append('reset HEAD --hard')
self.cmd.append('fetch origin')
self.cmd.append('checkout -f '+branchname)
else:
self.cmd.append('branch')
return(self)
def merge(self, branchname):
self.cmd.append('merge '+branchname)
return(self)
def tag(self, tagname):
os.chdir(self.workspace)
self.cmd.append('tag ' + tagname)
return(self)
def checkout(self, revision=None):
os.chdir(self.workspace)
if revision :
self.cmd.append('checkout -f '+revision)
return(self)
def debug(self):
cmd = ''
for line in self.cmd:
cmd += 'git ' + line + '; '
return(cmd)
def execute(self):
for line in self.cmd:
os.system('git '+ line)
self.logging.debug('git '+ line)
self.cmd = []
print("-")
1 change: 1 addition & 0 deletions library/whiptail.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#-*- coding: utf-8 -*-
import os,subprocess

class Whiptail():
Expand Down
17 changes: 9 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
# pass

setup(
name='oscm',
name='devops',
version=__version__,
description="Collection of useful functions and classes",
description="DevOps of useful deployment and automation",
long_description=readme + '\n\n' + changes,
keywords='oscm',
keywords='devops',
author=__author__,
author_email='netkiller@msn.com',
url='http://netkiller.github.io',
Expand All @@ -34,7 +34,7 @@
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
],
package_dir={ '': 'library' },
packages=[
Expand All @@ -44,16 +44,17 @@
'bin/deployment',
'bin/backup',
'bin/mysqlshell',
'bin/auditlog'
'bin/auditlog',
'bin/gitsync'
],
data_files = [
('etc', ['etc/deployment.cfg']),
('etc', ['etc/task.cfg']),
('etc', ['etc/schedule.cfg']),
('log', ['log/deployment.log']),
('project/testing', ['project/testing/example.com.ini']),
('project/config/testing', ['project/config/testing/www.example.com.ini']),
('project/exclude/testing', ['project/exclude/testing/www.example.com.lst'])
('example/testing', ['example/testing/example.com.ini']),
('example/config/testing', ['example/config/testing/www.example.com.ini']),
('example/exclude/testing', ['example/exclude/testing/www.example.com.lst'])

]
)
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions share/example/production/x64.me.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[bg7nyt]
repository=https://github.com/netkiller/phalcon.git
remote= 192.168.6.1
destination=x64.me/bg7nyt.x64.me
logfile=/tmp/bg7nyt.x64.me.log
delete=Y
File renamed without changes.

0 comments on commit 22ba053

Please sign in to comment.