Skip to content

Commit

Permalink
Merge pull request #53 from kubespray/verbose_kube-version_options
Browse files Browse the repository at this point in the history
add options '--verbose' and '--kube-version'
  • Loading branch information
Smana authored Jul 9, 2016
2 parents bce1f12 + f8a7a76 commit a310a2e
Show file tree
Hide file tree
Showing 9 changed files with 764 additions and 38 deletions.
674 changes: 674 additions & 0 deletions LICENCE

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,48 +83,48 @@ frequently </br>

Basic usage
-----------

### Generate inventory for a baremetal cluster

If the servers are already available you can use the argument **prepare**
The command below will just clone the git repository and creates the
inventory.
The hostvars must be separated by a **comma without spaces**

kargo prepare --nodes node1[ansible_ssh_host=10.99.21.1] node2[ansible_ssh_host=10.99.21.2] node3[ansible_ssh_host=10.99.21.3]

### Run instances and generate the inventory on Clouds

**Note**: You may want to choose the architecture of your cluster. </br>
Here are 3 examples:
* 3 vms, all 3 have etcd installed, all 3 are nodes (running pods), 2 of them run master components
```
kargo aws --nodes 3 --nodes-instance-type t2.small
kargo [prepare|aws|gce] --nodes 3 --nodes-instance-type t2.small
```

![3nodes](https://s32.postimg.org/8q7gns8ut/3nodes.png)
* 6 vms, 2 are nodes and masters, 1 is node only and a distinct etcd cluster
```
kargo aws --nodes 3 --etcds 3 --etcds-instance-type t2.micro
kargo [prepare|aws|gce] --nodes 3 --etcds 3 --etcds-instance-type t2.micro
```

![3nodes3etcds](https://s32.postimg.org/hphgxcjmt/3nodes_3etcds.png)
* 8 vms, 2 distinct masters, 3 nodes and 3 etcds
```
kargo aws --nodes 3 --etcds 3 --masters 2
kargo [prepare|aws|gce] --nodes 3 --etcds 3 --masters 2
```

![3nodes3etcds2masters](https://s31.postimg.org/h4gdu4qjv/3nodes_2masters_3etcds.png)

You should have at least 3 nodes but you can spawn only one instance for
tests purposes.

### Generate inventory for a baremetal cluster

If the servers are already available you can use the argument **prepare**
The command below will just clone the git repository and creates the
inventory.
The hostvars must be separated by a **comma without spaces**

kargo prepare --nodes node1[ansible_ssh_host=10.99.21.1] node2[ansible_ssh_host=10.99.21.2] node3[ansible_ssh_host=10.99.21.3] [--etcds N+] [--masters N+]

### Run instances and generate the inventory on Clouds


**AWS**

In order to create vms on AWS you can either edit the config file */etc/kargo/kargo.yml* or set the options with the argument **aws**
if the config file is filled with the proper information you just need to run the following command

kargo aws --nodes 3
kargo aws --nodes 3 [--etcds N+] [masters N+] [--nodes-instance-type m4.large]

Another example which download kargo's repo in a defined directory and set the cluster name

Expand Down
28 changes: 20 additions & 8 deletions bin/kargo
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.

__version__ = '0.4.2'
__version__ = '0.4.4'

import os
import argparse
Expand All @@ -25,7 +25,7 @@ try:
from ansible.utils.display import Display
except ImportError:
raise ImportError('Cannot find Ansible: Please check your installation (required version 2)')
from kargo.common import clone_kargo_git_repo, read_password
from kargo.common import clone_kargo_git_repo
from kargo.configure import Config
from kargo.inventory import CfgInventory
from kargo.deploy import RunPlaybook
Expand Down Expand Up @@ -96,15 +96,18 @@ if __name__ == '__main__':
parent_parser = argparse.ArgumentParser(add_help=False)
parent_parser.add_argument(
'-p', '--path', dest='kargo_path',
help='Where the Ansible playbooks are installed'
help='Where the Ansible playbooks are installed. Default: ~/.kargo'
)
parent_parser.add_argument(
'--config', dest='configfile', help="Config file path. Defaults to /etc/kargo/kargo.yml"
)
parent_parser.add_argument('--config', dest='configfile', help="Config file")
parent_parser.add_argument(
'-y', '--assumeyes', default=False, dest='assume_yes', action='store_true',
help='When a yes/no prompt would be presented, assume that the user entered "yes"'
)
parent_parser.add_argument(
'-i', '--inventory', dest='inventory_path', help='Inventory file path'
'-i', '--inventory', dest='inventory_path',
help='Inventory file path. Defaults to <path parameter>/inventory/inventory.cfg'
)

# prepare
Expand Down Expand Up @@ -157,7 +160,8 @@ if __name__ == '__main__':
'--security-group-name', dest='security_group_name', help='AWS security group Name'
)
sg_group.add_argument(
'--security-group-id', dest='security_group_id', help='AWS security group ID'
'--security-group-id', dest='security_group_id',
help='AWS security group ID. Uses the "default" security group if not specified.'
)
aws_parser.add_argument(
'--assign-public-ip', default=None, dest='assign_public_ip', action='store_true',
Expand All @@ -167,9 +171,9 @@ if __name__ == '__main__':
'--use-private-ip', default=None, dest='use_private_ip', action='store_true',
help='If set to true, using the private ip for SSH connection'
)
aws_parser.add_argument('--vpc-id', dest='aws_vpc_id', help='EC2 VPC id')
aws_parser.add_argument('--vpc-id', dest='aws_vpc_id', help='EC2 VPC ID')
aws_parser.add_argument(
'--vpc-subnet', dest='vpc_subnet_id', help='EC2 VPC regional subnet'
'--vpc-subnet', dest='vpc_subnet_id', help='EC2 VPC subnet ID'
)
aws_parser.add_argument('--ami', dest='ami', help='AWS AMI')
aws_parser.add_argument(
Expand Down Expand Up @@ -311,6 +315,10 @@ if __name__ == '__main__':
'deploy', parents=[parent_parser],
help='Create GCE machines and generate inventory'
)
deploy_parser.add_argument(
'--verbose', default=False, action='store_true',
help="Run Ansible playbook in verbose mode '-vvvv'"
)
deploy_parser.add_argument(
'-k', '--sshkey', dest='ssh_key',
help='ssh key for authentication on remote servers'
Expand All @@ -327,6 +335,10 @@ if __name__ == '__main__':
'-P', '--prompt-passwd', default=False, action='store_true', dest='prompt_pwd',
help="Set the 'kube' passwd to authenticate to the API (Interactive mode)"
)
deploy_parser.add_argument(
'-V', '--kube-version', dest='kube_version',
help='Choose the kubernetes version to be installed'
)
deploy_parser.add_argument(
'-N', '--kube-network', dest='kube_network',
help="""Network to be used inside the cluster (/16),
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

requirements = [
'cffi>=1.6.0',
'setuptools>=5.5.1',
'setuptools>=11.3',
'cryptography>=1.3.2',
'requests>=2.4.3',
'netaddr>=0.7.18',
'markupsafe>=0.23',
'pyyaml>=3.11',
'pyasn1>=0.1.8',
'boto>=2.40.0',
'apache-libcloud>=0.20.1'
]
Expand All @@ -22,7 +22,7 @@

setup(
name='kargo',
version='0.4.2',
version='0.4.4',
description="Kargo kubernetes cluster deployment",
author="Smaine Kahlouch",
author_email='[email protected]',
Expand Down
2 changes: 1 addition & 1 deletion src/kargo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# (c) 2016, Smaine Kahlouch <[email protected]>
__author__ = 'smana'
__version__ = '0.4.2'
__version__ = '0.4.4'
2 changes: 1 addition & 1 deletion src/kargo/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def parse_configfile(self):
def default_values(self, args, config):
# Set kargo_path
if 'kargo_path' not in config.keys() and args.kargo_path is None:
config['kargo_path'] = os.path.join(os.path.expanduser("~"), 'kargo')
config['kargo_path'] = os.path.join(os.path.expanduser("~"), '.kargo')
arguments = dict(args._get_kwargs())
for key, value in arguments.items():
if value is not None:
Expand Down
40 changes: 38 additions & 2 deletions src/kargo/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import re
import sys
import os
import yaml
import signal
import netaddr
from subprocess import PIPE, STDOUT, Popen, check_output, CalledProcessError
Expand Down Expand Up @@ -122,6 +123,8 @@ def check_ping(self):
'-b', '--become-user=root', '-m', 'ping', 'all',
'-i', self.inventorycfg
]
if 'sshkey' in self.options.keys():
cmd = cmd + ['--private-key', self.options['sshkey']]
if self.options['coreos']:
cmd = cmd + ['-e', 'ansible_python_interpreter=/opt/bin/python']
rcode, emsg = run_command('SSH ping hosts', cmd)
Expand Down Expand Up @@ -178,6 +181,26 @@ def get_subnets(self):
svc_network = list(net.subnet(svc_pfx))[0]
return(svc_network, pods_network)

def read_kube_versions(self):
"""
Read the kubernetes versions from karo's vars file
"""
kube_vers_file = os.path.join(
self.options['kargo_path'] + '/roles/download/vars/kube_versions.yml'
)
try:
with open(kube_vers_file, "r") as f:
kube_versions_vars = yaml.load(f)
except IOError as e:
display.error('Cannot read kube_versions file %s: %s'
% (kube_vers_file, e)
)
sys.exit(1)
kube_versions = list()
for i in kube_versions_vars['kube_checksum']:
kube_versions.append(i)
return kube_versions

def deploy_kubernetes(self):
'''
Run the ansible playbook command
Expand All @@ -190,9 +213,9 @@ def deploy_kubernetes(self):
]
# Configure network plugin if defined
if 'network_plugin' in self.options.keys():
cmd = cmd + [ '-e',
cmd = cmd + ['-e',
'kube_network_plugin=%s' % self.options['network_plugin']
]
]
# Configure the network subnets pods and k8s services
if 'kube_network' in self.options.keys():
if not validate_cidr(self.options['kube_network'], version=4):
Expand All @@ -204,9 +227,22 @@ def deploy_kubernetes(self):
'-e', 'kube_service_addresses=%s' % svc_network.cidr,
'-e', 'kube_pods_subnet=%s' % pods_network
]
# Set kubernetes version
if 'kube_version' in self.options.keys():
available_kube_versions = self.read_kube_versions()
if self.options['kube_version'] not in available_kube_versions:
display.error(
'Kubernetes version %s is not supported, available versions = %s' %
(self.options['kube_version'], ','.join(available_kube_versions))
)
sys.exit(1)
cmd = cmd + ['-e', 'kube_version=%s' % self.options['kube_version']]
# Add root password for the apiserver
if 'k8s_passwd' in self.options.keys():
cmd = cmd + ['-e', 'kube_api_pwd=%s' % self.options['k8s_passwd']]
# Ansible verbose mode
if 'verbose' in self.options.keys() and self.options['verbose']:
cmd = cmd + ['-vvvv']
# Add any additionnal Ansible option
if 'ansible_opts' in self.options.keys():
cmd = cmd + self.options['ansible_opts'].split(' ')
Expand Down
2 changes: 1 addition & 1 deletion src/kargo/files/kargo.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
# Path where the kargo ansible playbooks will be installed
# Defaults to current user's home directory if not set
# kargo_path: "/tmp"
# kargo_path: "~/.kargo"

# Default inventory path
kargo_git_repo: "https://github.com/kubespray/kargo.git"
Expand Down
16 changes: 10 additions & 6 deletions src/kargo/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,16 @@ def format_inventory(self, masters, nodes, etcds):
)
elif self.platform == 'metal':
for host in nodes + masters + etcds:
r = re.search('(^.*)\[(.*)\]', host)
inventory_hostname = r.group(1)
var_str = r.group(2)
hostvars = list()
for var in var_str.split(','):
hostvars.append({'name': var.split('=')[0], 'value': var.split('=')[1]})
if '[' in host:
r = re.search('(^.*)\[(.*)\]', host)
inventory_hostname = r.group(1)
var_str = r.group(2)
hostvars = list()
for var in var_str.split(','):
hostvars.append({'name': var.split('=')[0], 'value': var.split('=')[1]})
else:
inventory_hostname = host
hostvars = []
new_inventory['all']['hosts'].append(
{'hostname': inventory_hostname, 'hostvars': hostvars}
)
Expand Down

0 comments on commit a310a2e

Please sign in to comment.