refactor
This commit is contained in:
parent
b96277bdb1
commit
4ef52dca44
6 changed files with 82 additions and 32 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1,3 @@
|
||||||
|
*.tfstate
|
||||||
|
*.tfstate.backup
|
||||||
*.pyc
|
*.pyc
|
1
VERSION
Normal file
1
VERSION
Normal file
|
@ -0,0 +1 @@
|
||||||
|
0.0.1
|
|
@ -11,13 +11,16 @@ class Terraform:
|
||||||
self.targets = [] if targets is None else targets
|
self.targets = [] if targets is None else targets
|
||||||
self.variables = dict() if variables is None else variables
|
self.variables = dict() if variables is None else variables
|
||||||
|
|
||||||
self.state = state
|
self.state_filename = state
|
||||||
self.state_data = None
|
self.state_data = dict()
|
||||||
self.parallelism = 50
|
self.parallelism = 50
|
||||||
|
|
||||||
def apply(self, targets=None, variables=None):
|
def apply(self, targets=None, variables=None, **kargs):
|
||||||
"""
|
"""
|
||||||
refer to https://terraform.io/docs/commands/apply.html
|
refer to https://terraform.io/docs/commands/apply.html
|
||||||
|
:param variables: variables in dict type
|
||||||
|
:param targets: targets in list
|
||||||
|
:returns return_code, stdout, stderr
|
||||||
"""
|
"""
|
||||||
variables = self.variables if variables is None else variables
|
variables = self.variables if variables is None else variables
|
||||||
targets = self.targets if targets is None else targets
|
targets = self.targets if targets is None else targets
|
||||||
|
@ -25,13 +28,20 @@ class Terraform:
|
||||||
parameters = []
|
parameters = []
|
||||||
parameters += self._generate_targets(targets)
|
parameters += self._generate_targets(targets)
|
||||||
parameters += self._generate_var_string(variables)
|
parameters += self._generate_var_string(variables)
|
||||||
parameters += ['-parallelism=%s' % self.parallelism]
|
parameters += self._gen_param_string(kargs)
|
||||||
|
|
||||||
parameters = \
|
parameters = \
|
||||||
['terraform', 'apply', '-state=%s' % self.state] + parameters
|
['terraform', 'apply', '-state=%s' % self.state_filename] + parameters
|
||||||
|
|
||||||
cmd = ' '.join(parameters)
|
cmd = ' '.join(parameters)
|
||||||
return self._run_cmd(cmd)
|
return self._run_cmd(cmd)
|
||||||
|
|
||||||
|
def _gen_param_string(self, kargs):
|
||||||
|
params = []
|
||||||
|
for key, value in kargs.items():
|
||||||
|
params += ['%s=%s' % (key, value)]
|
||||||
|
return params
|
||||||
|
|
||||||
def _run_cmd(self, cmd):
|
def _run_cmd(self, cmd):
|
||||||
log.debug('command: ' + cmd)
|
log.debug('command: ' + cmd)
|
||||||
|
|
||||||
|
@ -43,10 +53,10 @@ class Terraform:
|
||||||
|
|
||||||
if ret_code == 0:
|
if ret_code == 0:
|
||||||
log.debug('error: ' + err)
|
log.debug('error: ' + err)
|
||||||
self.read_state()
|
self.read_state_file()
|
||||||
return ret_code, out, err
|
return ret_code, out, err
|
||||||
|
|
||||||
def destroy(self, targets=None, variables=None):
|
def destroy(self, targets=None, variables=None, **kwargs):
|
||||||
variables = self.variables if variables is None else variables
|
variables = self.variables if variables is None else variables
|
||||||
targets = self.targets if targets is None else targets
|
targets = self.targets if targets is None else targets
|
||||||
|
|
||||||
|
@ -55,7 +65,7 @@ class Terraform:
|
||||||
parameters += self._generate_var_string(variables)
|
parameters += self._generate_var_string(variables)
|
||||||
|
|
||||||
parameters = \
|
parameters = \
|
||||||
['terraform', 'destroy', '-force', '-state=%s' % self.state] + \
|
['terraform', 'destroy', '-force', '-state=%s' % self.state_filename] + \
|
||||||
parameters
|
parameters
|
||||||
cmd = ' '.join(parameters)
|
cmd = ' '.join(parameters)
|
||||||
return self._run_cmd(cmd)
|
return self._run_cmd(cmd)
|
||||||
|
@ -68,15 +78,18 @@ class Terraform:
|
||||||
parameters += self._generate_targets(targets)
|
parameters += self._generate_targets(targets)
|
||||||
parameters += self._generate_var_string(variables)
|
parameters += self._generate_var_string(variables)
|
||||||
parameters = \
|
parameters = \
|
||||||
['terraform', 'refresh', '-state=%s' % self.state] + \
|
['terraform', 'refresh', '-state=%s' % self.state_filename] + \
|
||||||
parameters
|
parameters
|
||||||
cmd = ' '.join(parameters)
|
cmd = ' '.join(parameters)
|
||||||
return self._run_cmd(cmd)
|
return self._run_cmd(cmd)
|
||||||
|
|
||||||
def read_state(self, state=None):
|
def read_state_file(self):
|
||||||
state = self.state if state is None else state
|
"""
|
||||||
if os.path.exists(state):
|
read .tfstate file
|
||||||
with open(state) as f:
|
:return: states file in dict type
|
||||||
|
"""
|
||||||
|
if os.path.exists(self.state_filename):
|
||||||
|
with open(self.state_filename) as f:
|
||||||
json_data = json.load(f)
|
json_data = json.load(f)
|
||||||
self.state_data = json_data
|
self.state_data = json_data
|
||||||
log.debug("state_data=%s" % str(self.state_data))
|
log.debug("state_data=%s" % str(self.state_data))
|
||||||
|
@ -86,11 +99,11 @@ class Terraform:
|
||||||
|
|
||||||
def is_any_aws_instance_alive(self):
|
def is_any_aws_instance_alive(self):
|
||||||
self.refresh()
|
self.refresh()
|
||||||
if not os.path.exists(self.state):
|
if not os.path.exists(self.state_filename):
|
||||||
log.debug("can't find %s " % self.state_data)
|
log.debug("can't find %s " % self.state_data)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.read_state()
|
self.read_state_file()
|
||||||
try:
|
try:
|
||||||
main_module = self._get_main_module()
|
main_module = self._get_main_module()
|
||||||
for resource_key, info in main_module['resources'].items():
|
for resource_key, info in main_module['resources'].items():
|
||||||
|
@ -136,6 +149,11 @@ class Terraform:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_output_value(self, output_name):
|
def get_output_value(self, output_name):
|
||||||
|
"""
|
||||||
|
|
||||||
|
:param output_name:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
main_module = self._get_main_module()
|
main_module = self._get_main_module()
|
||||||
return main_module['outputs'][output_name]
|
return main_module['outputs'][output_name]
|
16
setup.py
16
setup.py
|
@ -5,19 +5,29 @@ from distutils.core import setup
|
||||||
import os
|
import os
|
||||||
|
|
||||||
dependencies = []
|
dependencies = []
|
||||||
module_name = 'terraform'
|
module_name = 'python-terraform'
|
||||||
|
|
||||||
|
|
||||||
|
def get_version():
|
||||||
|
p = os.path.join(os.path.dirname(
|
||||||
|
os.path.abspath(__file__)), "VERSION")
|
||||||
|
with open(p) as f:
|
||||||
|
version = f.read()
|
||||||
|
version = version.strip()
|
||||||
|
if not version:
|
||||||
|
raise ValueError("could not read version")
|
||||||
|
return version
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name=module_name,
|
name=module_name,
|
||||||
version="0.0.1",
|
version=get_version(),
|
||||||
url='https://github.com/beelit94/python-terraform',
|
url='https://github.com/beelit94/python-terraform',
|
||||||
license='BSD',
|
license='BSD',
|
||||||
author='Freddy Tan',
|
author='Freddy Tan',
|
||||||
author_email='beelit94@gmail.com',
|
author_email='beelit94@gmail.com',
|
||||||
description='This is a python module provide a wrapper of terraform command line tool',
|
description='This is a python module provide a wrapper of terraform command line tool',
|
||||||
long_description=__doc__,
|
long_description=__doc__,
|
||||||
packages=[module_name],
|
packages=['python_terraform'],
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
package_data={},
|
package_data={},
|
||||||
zip_safe=False,
|
zip_safe=False,
|
||||||
|
|
24
test/test.tf
24
test/test.tf
|
@ -0,0 +1,24 @@
|
||||||
|
variable "access_key" {}
|
||||||
|
variable "secret_key" {}
|
||||||
|
|
||||||
|
provider "aws" {
|
||||||
|
access_key = "${var.access_key}"
|
||||||
|
secret_key = "${var.secret_key}"
|
||||||
|
region = "us-west-2"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_instance" "ubuntu-1404" {
|
||||||
|
ami = "ami-9abea4fb"
|
||||||
|
instance_type = "t2.micro"
|
||||||
|
security_groups = ["terraform-salty-splunk"]
|
||||||
|
tags {
|
||||||
|
Name = "python-terraform-test"
|
||||||
|
}
|
||||||
|
// key_name = "${aws_key_pair.key.key_name}"
|
||||||
|
// connection {
|
||||||
|
// type = "ssh"
|
||||||
|
// user = "ubuntu"
|
||||||
|
// key_file = "${var.key_path}"
|
||||||
|
// timeout = "10m"
|
||||||
|
// }
|
||||||
|
}
|
|
@ -1,20 +1,15 @@
|
||||||
from terraform import Terraform
|
from python_terraform import Terraform
|
||||||
|
|
||||||
|
|
||||||
class TestTerraform:
|
class TestTerraform:
|
||||||
def test_apply(self):
|
def test_apply_and_destory(self):
|
||||||
tf = Terraform()
|
tf = Terraform()
|
||||||
|
ret_code, out, err = tf.apply()
|
||||||
|
|
||||||
tf.apply()
|
print out
|
||||||
|
print err
|
||||||
def test_refresh(self):
|
# assert ret_code, 0
|
||||||
tf = Terraform()
|
|
||||||
|
|
||||||
tf.refresh()
|
|
||||||
|
|
||||||
def test_destroy(self):
|
|
||||||
tf = Terraform()
|
|
||||||
|
|
||||||
tf.destroy()
|
|
||||||
|
|
||||||
|
ret_code, out, err = tf.destroy()
|
||||||
|
|
||||||
|
assert ret_code, 0
|
||||||
|
|
Loading…
Reference in a new issue