Skip to content

Commit

Permalink
Merge pull request #50 from anhou/enhancement/render
Browse files Browse the repository at this point in the history
Decouple tftp server and switch template
  • Loading branch information
Andrew Hou committed Aug 29, 2016
2 parents 0857aca + 5f0e39f commit e51de03
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
15 changes: 11 additions & 4 deletions data/templates/cisco-poap.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/env python
# Copyright 2016, EMC, Inc.
# Note, the following checksum currently has to be hand generated and done
# AFTER the profile has been rendered with the switchProfileUri value until
# AFTER the profile has been rendered with the apiServerAddress and apiServerPort value until
# RackHD can taught to handle this kind of render-chain.
# f=cisco-poap.py ; cat $f | sed '/^#md5sum/d' > $f.md5 ; sed -i "s/^#md5sum=.*/#md5sum=\"$(md5sum $f.md5 | sed 's/ .*//')\"/" $f

Expand All @@ -19,12 +19,19 @@
except:
from cisco import cli

API_SERVER_ADDRESS = '<%=apiServerAddress%>'
API_SERVER_PORT = '<%=apiServerPort%>'

switch_profile_uri = 'http://{0}:{1}/api/1.1/profiles/switch'.format(API_SERVER_ADDRESS, API_SERVER_PORT)
switch_profile_error_uri = '{0}/error/'.format(switch_profile_uri)
cisco_switch_profile_uri = '{0}/cisco'.format(switch_profile_uri)

def download_cisco_script(context):
# Add switch vendor string as the last URI element as a hint to the
# HTTP API server
poap_log("Downloading script <%=switchProfileUri%>/cisco - vrf %s" % context)
poap_log("Downloading script %s - vrf %s" % (cisco_switch_profile_uri, context))
cisco.vrf.set_global_vrf(context)
script = urllib2.urlopen('<%=switchProfileUri%>/cisco').read()
script = urllib2.urlopen(cisco_switch_profile_uri).read()
poap_log("Opening script rackhd_cisco_script.py")
poap_log(script)
with open('rackhd_cisco_script.py', 'w') as rackhd_cisco_script:
Expand Down Expand Up @@ -75,7 +82,7 @@ def abort_cleanup_exit () :
except Exception as e:
data = json.dumps({"error": traceback.format_exc()})
cisco.vrf.set_global_vrf(good_context)
req = urllib2.Request('<%=switchProfileErrorUri%>',
req = urllib2.Request(switch_profile_error_uri,
data, {"Content-Type": "application/json"})
urllib2.urlopen(req)
# Don't swallow exceptions otherwise the Cisco switch will think the POAP was a success
Expand Down
12 changes: 5 additions & 7 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ function TftpServerFactory(
this.host = configuration.get('tftpBindAddress', '0.0.0.0');
this.port = configuration.get('tftpBindPort', 69);
this.root = configuration.get('tftpRoot', './static/tftp');
var switchProfileUri = 'http://%s:%s/api/1.1/profiles/switch'.format(
configuration.get('apiServerAddress', '10.1.1.1'),
configuration.get('apiServerPort', '9080')
);
var switchProfileErrorUri = switchProfileUri + '/error/';
var apiServerAddress = configuration.get('apiServerAddress', '10.1.1.1');
var apiServerPort = configuration.get('apiServerPort', '9080');

this.renderContext = {
switchProfileUri: switchProfileUri,
switchProfileErrorUri: switchProfileErrorUri
apiServerAddress: apiServerAddress,
apiServerPort: apiServerPort
};
this.templates = {};

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/server-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('tftp server tests', function () {
};
server.templates = {
test: {
contents: '<%=switchProfileUri%> <%=switchProfileErrorUri%>',
contents: '<%=apiServerAddress%> <%=apiServerPort%>',
path: 'testpath'
}
};
Expand All @@ -118,9 +118,9 @@ describe('tftp server tests', function () {
var rendered = res.end.firstCall.args[0];
var size = res.setSize.firstCall.args[0];

var expected = server.renderContext.switchProfileUri +
var expected = server.renderContext.apiServerAddress +
' ' +
server.renderContext.switchProfileErrorUri;
server.renderContext.apiServerPort;
expect(rendered).to.equal(expected);
expect(size).to.equal(expected.length);
});
Expand Down

0 comments on commit e51de03

Please sign in to comment.