4
4
import os
5
5
import re
6
6
import sys
7
- import fileinput
8
-
9
- if (len (sys .argv ) < 2 ):
10
- print "You must give a new environment name"
11
- sys .exit ()
12
- else :
13
- new_env = sys .argv [1 ]
14
7
15
8
def get_current_env ():
16
9
# Use regex to determine what MAXIS_ENV is currently set to
17
10
etc_profile = open ("/etc/profile" ,"r" )
18
- regex = re .compile ('MAXIS_ENV=(\w+)' )
11
+ regex = re .compile ('MAXIS_ENV=(\w+)$ ' )
19
12
for line in etc_profile :
20
13
env = regex .findall (line )
21
- #etc_profile.close()
22
- #print 'MAXIS_ENV is currently set to "%s"' % env[0]
23
14
return env [0 ]
24
15
25
16
def update_file (file ,current_env ,new_env ):
@@ -32,18 +23,45 @@ def update_file(file,current_env,new_env):
32
23
# Also open a file handle on the same file for writing
33
24
new_file = open (file ,'w' )
34
25
# Search and replace
35
- changed_data = current_file .replace (current_env ,new_env )
26
+ full_current_env = "export MAXIS_ENV=" + current_env
27
+ full_new_env = "export MAXIS_ENV=" + new_env
28
+ changed_data = current_file .replace (full_current_env ,full_new_env )
36
29
# Write out the changed data to the file
37
30
new_file .write (changed_data )
38
31
#current_file.close()
39
32
new_file .close ()
40
33
current_env = get_current_env ()
41
34
print 'MAXIS_ENV in %s has been changed to "%s"' % (file ,current_env )
42
35
36
+ def usage ():
37
+ print ' ---------------------------------------------------------------------------'
38
+ print ''
39
+ print ' This script is used to change the environment settings to your particular '
40
+ print ' needs. By default, any instances built from this AMI are named "standard".'
41
+ print ' Using this script you can change it to whatever is needed or desired.'
42
+ print ' '
43
+ print ' Usage : ./change_env.py newEnvironmentName'
44
+ print ' '
45
+ print ' '
46
+ print ' ---------------------------------------------------------------------------'
47
+ print ' '
48
+
49
+ def main (new_env ):
50
+ current_env = get_current_env ()
51
+ print ""
52
+ print " ---------------------------------------------------------------------------"
53
+ print ""
54
+ print 'Currently MAXIX_ENV is set to "%s"' % current_env
55
+ print ""
56
+ update_file ("/etc/profile" ,current_env ,new_env )
57
+ update_file ("/usr/bin/maxisenv" ,current_env ,new_env )
58
+ print ""
59
+ print " ---------------------------------------------------------------------------"
60
+ print ""
61
+
43
62
44
- current_env = get_current_env ()
45
- print ""
46
- print 'Currently MAXIX_ENV is set to "%s"' % current_env
47
- update_file ("/etc/profile" ,current_env ,new_env )
48
- update_file ("/usr/bin/maxisenv" ,current_env ,new_env )
49
- print ""
63
+ if (len (sys .argv ) < 2 ):
64
+ usage ()
65
+ else :
66
+ new_env = sys .argv [1 ]
67
+ main (new_env )
0 commit comments