Skip to content

Commit bf78531

Browse files
radomirbosakrbosak
authored andcommitted
Support multiple Jenkins instances in config file
This commit adds sections to the .jenkins-cli config file. The section which should be used is specified with the -e or --environment. If no environment is specified, the "DEFAULT" section is used. Fixes #36
1 parent 58db8f4 commit bf78531

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,15 @@ Host, username and password may be specified either by the command line argument
3838

3939
**.jenkins-cli** example
4040
```txt
41+
[DEFAULT]
4142
host=http://localhost:8082/
4243
username=username
4344
password=******
45+
46+
[prod]
47+
host=https://production-jenkins.example.com/
48+
username=username
49+
password=xxxxxx
4450
```
4551

4652
# Commands overview:

jenkins_cli/cli.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
import socket
88
from xml.etree import ElementTree
99

10+
try:
11+
from ConfigParser import ConfigParser, NoSectionError
12+
except ImportError:
13+
from configparser import ConfigParser, NoSectionError
14+
15+
1016
STATUSES_COLOR = {'blue': {'symbol': 'S',
1117
'color': '\033[94m',
1218
'descr': 'Stable'},
@@ -87,12 +93,13 @@ class JenkinsCli(object):
8793
"%s branch set to: %s")
8894

8995
def __init__(self, args, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
90-
self.jenkins = self.auth(args.host, args.username, args.password, timeout)
96+
self.jenkins = self.auth(args.host, args.username, args.password,
97+
args.environment, timeout)
9198

9299
@classmethod
93-
def auth(cls, host=None, username=None, password=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
100+
def auth(cls, host=None, username=None, password=None, environment=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
94101
if host is None or username is None or password is None:
95-
settings_dict = cls.read_settings_from_file()
102+
settings_dict = cls.read_settings_from_file(environment)
96103
try:
97104
host = host or settings_dict['host']
98105
username = username or settings_dict.get('username', None)
@@ -102,26 +109,32 @@ def auth(cls, host=None, username=None, password=None, timeout=socket._GLOBAL_DE
102109
return jenkins.Jenkins(host, username, password, timeout)
103110

104111
@classmethod
105-
def read_settings_from_file(cls):
106-
try:
107-
current_folder = os.getcwd()
108-
filename = os.path.join(current_folder, cls.SETTINGS_FILE_NAME)
112+
def read_settings_from_file(cls, environment):
113+
# get config filename
114+
current_folder = os.getcwd()
115+
filename = os.path.join(current_folder, cls.SETTINGS_FILE_NAME)
116+
if not os.path.exists(filename):
117+
home_folder = os.path.expanduser("~")
118+
filename = os.path.join(home_folder, cls.SETTINGS_FILE_NAME)
109119
if not os.path.exists(filename):
110-
home_folder = os.path.expanduser("~")
111-
filename = os.path.join(home_folder, cls.SETTINGS_FILE_NAME)
112-
if not os.path.exists(filename):
113-
return {}
114-
f = open(filename, 'r')
115-
jenkins_settings = f.read()
120+
return {}
121+
122+
# use the DEFAULT section if no env is specified
123+
if not environment:
124+
environment = 'DEFAULT'
125+
126+
# read the config file
127+
config = ConfigParser()
128+
try:
129+
config.readfp(open(filename, 'r'))
116130
except Exception as e:
117131
raise CliException('Error reading %s: %s' % (filename, e))
118132

119-
settings_dict = {}
120-
for setting_line in jenkins_settings.split('\n'):
121-
if "=" in setting_line:
122-
key, value = setting_line.split("=", 1)
123-
settings_dict[key.strip()] = value.strip()
124-
return settings_dict
133+
# return the variables as dict
134+
try:
135+
return dict(config.items(environment))
136+
except NoSectionError:
137+
raise CliException('No such environment: %s' % environment)
125138

126139
def run_command(self, args):
127140
command = args.jenkins_command

jenkins_cli/cli_arguments.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def load_parser():
2020
parser.add_argument('--username', metavar='username', help='Jenkins Username', default=None)
2121
parser.add_argument('--password', metavar='password', help='Jenkins Password', default=None)
2222
parser.add_argument('--version', '-v', action='version', version='jenkins-cli %s' % version)
23+
parser.add_argument('-e', '--environment',
24+
help='Which config section to use')
2325

2426
subparsers = parser.add_subparsers(title='Available commands', dest='jenkins_command')
2527

tests/test_cli.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ def test_auth_has_file_settings(self, patched_init, read_settings_from_file):
7575

7676

7777
class TestCliFileUsing(fake_filesystem_unittest.TestCase):
78-
HOME_FILE_CONTENT = ("host =https://jenkins.host.com\n"
78+
HOME_FILE_CONTENT = ("[DEFAULT]\n"
79+
"host =https://jenkins.host.com\n"
7980
"username= username\n"
8081
"some weird settings = value = value")
8182

82-
LOCAL_FILE_CONTENT = ("host=http://jenkins.localhosthost.ua\n"
83+
LOCAL_FILE_CONTENT = ("[DEFAULT]\n"
84+
"host=http://jenkins.localhosthost.ua\n"
8385
"username=Denys\n"
8486
"password=myPassword\n"
8587
"other_setting=some_value")
@@ -97,7 +99,7 @@ def test_read_settings_from_file(self):
9799
self.fs.CreateFile(home_folder_filename,
98100
contents=self.HOME_FILE_CONTENT)
99101
self.assertTrue(os.path.exists(home_folder_filename))
100-
settings_dict = JenkinsCli.read_settings_from_file()
102+
settings_dict = JenkinsCli.read_settings_from_file(environment=None)
101103
self.assertEqual(settings_dict,
102104
{"host": 'https://jenkins.host.com',
103105
"username": "username",
@@ -107,7 +109,7 @@ def test_read_settings_from_file(self):
107109
self.fs.CreateFile(local_folder_filename,
108110
contents=self.LOCAL_FILE_CONTENT)
109111
self.assertTrue(os.path.exists(local_folder_filename))
110-
settings_dict = JenkinsCli.read_settings_from_file()
112+
settings_dict = JenkinsCli.read_settings_from_file(environment=None)
111113
self.assertEqual(settings_dict,
112114
{"host": 'http://jenkins.localhosthost.ua',
113115
"username": "Denys",
@@ -119,7 +121,7 @@ def test_read_settings_from_file(self):
119121
class TestCliCommands(unittest.TestCase):
120122

121123
def setUp(self):
122-
self.args = Namespace(host='http://jenkins.host.com', username=None, password=None)
124+
self.args = Namespace(host='http://jenkins.host.com', username=None, password=None, environment=None)
123125
self.print_patcher = mock.patch('jenkins_cli.cli.print')
124126
self.patched_print = self.print_patcher.start()
125127

0 commit comments

Comments
 (0)