This commit is contained in:
Freddy Tan 2015-12-31 15:15:51 +08:00
parent a0975baeb1
commit 4ebdc607dc
4 changed files with 26 additions and 3 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.pyc

View file

@ -9,8 +9,9 @@ log = logging.getLogger(__name__)
class Terraform: class Terraform:
def __init__(self, targets=None, state='terraform.tfstate', variables=None): def __init__(self, targets=None, state='terraform.tfstate', variables=None):
self.targets = [] if targets is None else targets self.targets = [] if targets is None else targets
self.state = state
self.variables = dict() if variables is None else variables self.variables = dict() if variables is None else variables
self.state = state
self.state_data = None self.state_data = None
self.parallelism = 50 self.parallelism = 50
@ -33,11 +34,13 @@ class Terraform:
def _run_cmd(self, cmd): def _run_cmd(self, cmd):
log.debug('command: ' + cmd) log.debug('command: ' + cmd)
p = subprocess.Popen( p = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = p.communicate() out, err = p.communicate()
ret_code = p.returncode ret_code = p.returncode
log.debug('output: ' + out) log.debug('output: ' + out)
if ret_code == 0: if ret_code == 0:
log.debug('error: ' + err) log.debug('error: ' + err)
self.read_state() self.read_state()
@ -50,8 +53,7 @@ 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)
# hard code 19 for splunk aws limit on us-west-2 region,
# todo move parallelism?
parameters = \ parameters = \
['terraform', 'destroy', '-force', '-state=%s' % self.state] + \ ['terraform', 'destroy', '-force', '-state=%s' % self.state] + \
parameters parameters

0
test/__init__.py Normal file
View file

View file

@ -0,0 +1,20 @@
from terraform import Terraform
class TestTerraform:
def test_apply(self):
tf = Terraform()
tf.apply()
def test_refresh(self):
tf = Terraform()
tf.refresh()
def test_destroy(self):
tf = Terraform()
tf.destroy()