Merge pull request #8 from xetorthio/stack_name

Allow to specify stack name as a parameter
This commit is contained in:
Marcos Nils
2017-05-19 17:32:15 -03:00
committed by GitHub
4 changed files with 19 additions and 3 deletions

View File

@@ -27,6 +27,7 @@ func NewSession(rw http.ResponseWriter, req *http.Request) {
reqDur := req.Form.Get("session-duration") reqDur := req.Form.Get("session-duration")
stack := req.Form.Get("stack") stack := req.Form.Get("stack")
stackName := req.Form.Get("stack_name")
if stack != "" { if stack != "" {
stack = formatStack(stack) stack = formatStack(stack)
@@ -42,7 +43,7 @@ func NewSession(rw http.ResponseWriter, req *http.Request) {
} }
duration := services.GetDuration(reqDur) duration := services.GetDuration(reqDur)
s, err := services.NewSession(duration, stack) s, err := services.NewSession(duration, stack, stackName)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
//TODO: Return some error code //TODO: Return some error code

View File

@@ -59,6 +59,7 @@ type Session struct {
PwdIpAddress string `json:"pwd_ip_address"` PwdIpAddress string `json:"pwd_ip_address"`
Ready bool `json:"ready"` Ready bool `json:"ready"`
Stack string `json:"stack"` Stack string `json:"stack"`
StackName string `json:"stack_name"`
} }
type sessionBuilderWriter struct { type sessionBuilderWriter struct {
@@ -113,7 +114,7 @@ func (s *Session) DeployStack() error {
fileName := path.Base(s.Stack) fileName := path.Base(s.Stack)
file := fmt.Sprintf("/var/run/pwd/uploads/%s", fileName) file := fmt.Sprintf("/var/run/pwd/uploads/%s", fileName)
cmd := fmt.Sprintf("docker swarm init --advertise-addr eth0 && docker-compose -f %s pull && docker stack deploy -c %s pwd", file, file) cmd := fmt.Sprintf("docker swarm init --advertise-addr eth0 && docker-compose -f %s pull && docker stack deploy -c %s %s", file, file, s.StackName)
w := sessionBuilderWriter{session: s} w := sessionBuilderWriter{session: s}
code, err := ExecAttach(i.Name, []string{"sh", "-c", cmd}, &w) code, err := ExecAttach(i.Name, []string{"sh", "-c", cmd}, &w)
@@ -300,7 +301,7 @@ func GetDuration(reqDur string) time.Duration {
return defaultDuration return defaultDuration
} }
func NewSession(duration time.Duration, stack string) (*Session, error) { func NewSession(duration time.Duration, stack, stackName string) (*Session, error) {
s := &Session{} s := &Session{}
s.Id = uuid.NewV4().String() s.Id = uuid.NewV4().String()
s.Instances = map[string]*Instance{} s.Instances = map[string]*Instance{}
@@ -310,6 +311,10 @@ func NewSession(duration time.Duration, stack string) (*Session, error) {
s.Ready = true s.Ready = true
} }
s.Stack = stack s.Stack = stack
if stackName == "" {
stackName = "pwd"
}
s.StackName = stackName
log.Printf("NewSession id=[%s]\n", s.Id) log.Printf("NewSession id=[%s]\n", s.Id)
// Schedule cleanup of the session // Schedule cleanup of the session

View File

@@ -12,6 +12,7 @@
<form id="welcomeFormBypass" method="POST" action="/"> <form id="welcomeFormBypass" method="POST" action="/">
<button id="start" type="submit">Start Session</button> <button id="start" type="submit">Start Session</button>
<input id="stack" type="hidden" name="stack" value=""/> <input id="stack" type="hidden" name="stack" value=""/>
<input id="stack_name" type="hidden" name="stack_name" value=""/>
</form> </form>
</div> </div>
</body> </body>
@@ -37,5 +38,9 @@
if (stack) { if (stack) {
document.getElementById('stack').value = stack; document.getElementById('stack').value = stack;
} }
var stackName = getParameterByName('stack_name');
if (stack) {
document.getElementById('stack_name').value = stackName;
}
</script> </script>
</html> </html>

View File

@@ -15,6 +15,7 @@
<div id="recaptcha" class="g-recaptcha" data-callback="iAmHuman" data-sitekey="{{.}}"></div> <div id="recaptcha" class="g-recaptcha" data-callback="iAmHuman" data-sitekey="{{.}}"></div>
<input type="hidden" name="session-duration" value="4h"/> <input type="hidden" name="session-duration" value="4h"/>
<input id="stack" type="hidden" name="stack" value=""/> <input id="stack" type="hidden" name="stack" value=""/>
<input id="stack_name" type="hidden" name="stack_name" value=""/>
<button id="create" style="display:none;">Create session</button> <button id="create" style="display:none;">Create session</button>
</form> </form>
<img src="/assets/large_h.png" /> <img src="/assets/large_h.png" />
@@ -38,6 +39,10 @@
if (stack) { if (stack) {
document.getElementById('stack').value = stack; document.getElementById('stack').value = stack;
} }
var stackName = getParameterByName('stack_name');
if (stackName) {
document.getElementById('stack_name').value = stackName;
}
if (document.cookie.indexOf('session_id') > -1) { if (document.cookie.indexOf('session_id') > -1) {
document.getElementById('create').style = ""; document.getElementById('create').style = "";
document.getElementById('recaptcha').style = "display:none;"; document.getElementById('recaptcha').style = "display:none;";