Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shamanec committed Jan 16, 2022
1 parent 9c6161c commit e07d7b8
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 262 deletions.
172 changes: 8 additions & 164 deletions docker_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,167 +301,7 @@ func ImageExists() (imageStatus string) {
return
}

// @Summary Create iOS container
// @Description Creates a docker container for iOS device for provided device UDID
// @Tags ios-devices
// @Produce json
// @Param device_udid path string true "Device UDID"
// @Success 200 {object} SimpleResponseJSON
// @Failure 500 {object} ErrorJSON
// @Router /ios_containers/{device_udid}/create [post]
func CreateIOSContainer(w http.ResponseWriter, r *http.Request) {
// Get the parameters
vars := mux.Vars(r)
device_udid := vars["device_udid"]

log.WithFields(log.Fields{
"event": "ios_container_create",
}).Info("Attempting to create a container for iOS device with udid: " + device_udid)

error_message := "Could not create container for device with udid: " + device_udid

if !CheckIOSDeviceInDevicesList(device_udid) {
log.WithFields(log.Fields{
"event": "ios_container_create",
}).Warn("Device with udid: " + device_udid + " is not available in the attached devices list from go-ios.")
JSONError(w, "ios_container_create", "Device is not available in the attached devices list from go-ios.", 500)
return
}

ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
log.WithFields(log.Fields{
"event": "ios_container_create",
}).Error("Could not create docker client when attempting to create a container for device with udid: " + device_udid)
JSONError(w, "ios_container_create", error_message, 500)
return
}

jsonFile, err := os.Open("./configs/config.json")
if err != nil {
log.WithFields(log.Fields{
"event": "ios_container_create",
}).Error("Could not open ./configs/config.json when attempting to create a container for device with udid: " + device_udid)
JSONError(w, "ios_container_create", error_message, 500)
return
}
defer jsonFile.Close()

byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
log.WithFields(log.Fields{
"event": "ios_container_create",
}).Error("Could not read ./configs/config.json when attempting to create a container for device with udid: " + device_udid)
JSONError(w, "ios_container_create", error_message, 500)
return
}
appium_port := gjson.Get(string(byteValue), `devicesList.#(device_udid="`+device_udid+`").appium_port`)
device_name := gjson.Get(string(byteValue), `devicesList.#(device_udid="`+device_udid+`").device_name`)
device_os_version := gjson.Get(string(byteValue), `devicesList.#(device_udid="`+device_udid+`").device_os_version`)
wda_mjpeg_port := gjson.Get(string(byteValue), `devicesList.#(device_udid="`+device_udid+`").wda_mjpeg_port`)
wda_port := gjson.Get(string(byteValue), `devicesList.#(device_udid="`+device_udid+`").wda_port`)
wda_bundle_id := gjson.Get(string(byteValue), "wda_bundle_id")
selenium_hub_port := gjson.Get(string(byteValue), "selenium_hub_port")
selenium_hub_host := gjson.Get(string(byteValue), "selenium_hub_host")
devices_host := gjson.Get(string(byteValue), "devices_host")
hub_protocol := gjson.Get(string(byteValue), "hub_protocol")

config := &container.Config{
Image: "ios-appium",
ExposedPorts: nat.PortSet{
nat.Port(appium_port.Raw): struct{}{},
nat.Port(wda_port.Raw): struct{}{},
nat.Port(wda_mjpeg_port.Raw): struct{}{},
},
Env: []string{"ON_GRID=" + on_grid,
"DEVICE_UDID=" + device_udid,
"WDA_PORT=" + wda_port.Raw,
"MJPEG_PORT=" + wda_mjpeg_port.Raw,
"APPIUM_PORT=" + appium_port.Raw,
"DEVICE_OS_VERSION=" + device_os_version.Str,
"DEVICE_NAME=" + device_name.Str,
"WDA_BUNDLEID=" + wda_bundle_id.Str,
"SELENIUM_HUB_PORT=" + selenium_hub_port.Str,
"SELENIUM_HUB_HOST=" + selenium_hub_host.Str,
"DEVICES_HOST=" + devices_host.Str,
"HUB_PROTOCOL=" + hub_protocol.Str},
}

host_config := &container.HostConfig{
PortBindings: nat.PortMap{
nat.Port(appium_port.Raw): []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: appium_port.Raw,
},
},
nat.Port(wda_port.Raw): []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: wda_port.Raw,
},
},
nat.Port(wda_mjpeg_port.Raw): []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: wda_mjpeg_port.Raw,
},
},
},
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: "/var/run/usbmuxd",
Target: "/var/run/usbmuxd",
},
{
Type: mount.TypeBind,
Source: "/var/lib/lockdown",
Target: "/var/lib/lockdown",
},
{
Type: mount.TypeBind,
Source: project_dir + "/logs/container_" + device_name.Str + "-" + device_udid,
Target: "/opt/logs",
},
{
Type: mount.TypeBind,
Source: project_dir + "/ipa",
Target: "/opt/ipa",
},
},
}

err = os.MkdirAll("./logs/container_"+device_name.Str+"-"+device_udid, os.ModePerm)
if err != nil {
log.WithFields(log.Fields{
"event": "ios_container_create",
}).Error("Could not create logs folder for container. Error: " + err.Error())
JSONError(w, "ios_container_create", error_message, 500)
return
}

resp, err := cli.ContainerCreate(ctx, config, host_config, nil, nil, "ios_device_"+device_name.Str+"-"+device_udid)
if err != nil {
log.WithFields(log.Fields{
"event": "ios_container_create",
}).Error("Could not create container. Error: " + err.Error())
JSONError(w, "ios_container_create", error_message, 500)
return
}

err = cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
if err != nil {
log.WithFields(log.Fields{
"event": "ios_container_create",
}).Error("Could not start container. Error: " + err.Error())
JSONError(w, "ios_container_create", error_message, 500)
return
}
}

func CreateIOSContainerLocal(device_udid string) {
func CreateIOSContainer(device_udid string) {
log.WithFields(log.Fields{
"event": "ios_container_create",
}).Info("Attempting to create a container for iOS device with udid: " + device_udid)
Expand Down Expand Up @@ -607,6 +447,10 @@ func CreateIOSContainerLocal(device_udid string) {
}).Error("Could not start container for device with udid: " + device_udid + ". Error: " + err.Error())
return
}

log.WithFields(log.Fields{
"event": "ios_container_create",
}).Info("Successfully created a container for iOS device with udid: " + device_udid)
}

func UpdateIOSContainersLocal() {
Expand Down Expand Up @@ -653,7 +497,7 @@ func CreateIOSContainers(devices ios.DeviceList, containers []types.Container) {
}
}
if !device_has_container {
CreateIOSContainerLocal(device.Properties.SerialNumber)
CreateIOSContainer(device.Properties.SerialNumber)
}
}
}
Expand All @@ -668,7 +512,7 @@ func DestroyIOSContainers(devices ios.DeviceList, containers []types.Container)
}
}
if !container_has_device {
RemoveIOSContainerLocal(container.ID)
RemoveIOSContainer(container.ID)
}
}
}
Expand Down Expand Up @@ -732,7 +576,7 @@ func RemoveContainer(w http.ResponseWriter, r *http.Request) {
SimpleJSONResponse(w, "docker_container_remove", "Successfully removed container with ID: "+key, 200)
}

func RemoveIOSContainerLocal(container_id string) {
func RemoveIOSContainer(container_id string) {

log.WithFields(log.Fields{
"event": "docker_container_remove",
Expand Down
35 changes: 0 additions & 35 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,41 +359,6 @@ var doc = `{
}
}
},
"/ios_containers/{device_udid}/create": {
"post": {
"description": "Creates a docker container for iOS device for provided device UDID",
"produces": [
"application/json"
],
"tags": [
"ios-devices"
],
"summary": "Create iOS container",
"parameters": [
{
"type": "string",
"description": "Device UDID",
"name": "device_udid",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/main.SimpleResponseJSON"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/main.ErrorJSON"
}
}
}
}
},
"/project-logs": {
"get": {
"description": "Provides project logs as plain text response",
Expand Down
35 changes: 0 additions & 35 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -340,41 +340,6 @@
}
}
},
"/ios_containers/{device_udid}/create": {
"post": {
"description": "Creates a docker container for iOS device for provided device UDID",
"produces": [
"application/json"
],
"tags": [
"ios-devices"
],
"summary": "Create iOS container",
"parameters": [
{
"type": "string",
"description": "Device UDID",
"name": "device_udid",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/main.SimpleResponseJSON"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/main.ErrorJSON"
}
}
}
}
},
"/project-logs": {
"get": {
"description": "Provides project logs as plain text response",
Expand Down
23 changes: 0 additions & 23 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -242,29 +242,6 @@ paths:
summary: Get logs for iOS device container
tags:
- device-logs
/ios_containers/{device_udid}/create:
post:
description: Creates a docker container for iOS device for provided device UDID
parameters:
- description: Device UDID
in: path
name: device_udid
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/main.SimpleResponseJSON'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/main.ErrorJSON'
summary: Create iOS container
tags:
- ios-devices
/ios_containers/update:
post:
description: Creates (or removes respectively) iOS containers based on the connected
Expand Down
4 changes: 0 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ func handleRequests() {
))

// iOS containers endpoints
myRouter.HandleFunc("/ios-containers/{device_udid}/create", CreateIOSContainer)
myRouter.HandleFunc("/ios-containers/update", UpdateIOSContainers).Methods("POST")

// Android containers endpoints
Expand Down Expand Up @@ -270,9 +269,6 @@ func handleRequests() {
myRouter.HandleFunc("/project-logs.html", GetLogsPage)
myRouter.HandleFunc("/", GetInitialPage)

// Test endpoints
myRouter.HandleFunc("/test", CreateIOSContainer)

//log.Fatal(http.ListenAndServeTLS(":10000", "ca-cert.pem", "ca-key.pem", myRouter))
log.Fatal(http.ListenAndServe(":10000", myRouter))
}
Expand Down
1 change: 1 addition & 0 deletions static/android_containers.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<a href="ios-containers.html">iOS containers</a>
<a class="active" href="android-containers.html">Android containers</a>
<a href="project-logs.html">Project logs</a>
<a href="swagger/index.html" target="_blank">Swagger</a>
</div>
<table>
<tr>
Expand Down
3 changes: 2 additions & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<head>
<meta charset="UTF-8">
<title>Docker Info</title>
<title>Welcome page</title>
</head>

<body>
Expand All @@ -14,6 +14,7 @@
<a href="ios-containers.html">iOS containers</a>
<a href="android-containers.html">Android containers</a>
<a href="project-logs.html">Project logs</a>
<a href="swagger/index.html" target="_blank">Swagger</a>
</div>
<h1 style="margin: 15px;">Welcome</h1>
<!-- <h2>openssl req -x509 -newkey rsa:4096 -days 365 -nodes -keyout ca-key.pem -out ca-cert.pem</h2> -->
Expand Down
5 changes: 5 additions & 0 deletions static/ios_containers.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<a class="active" href="ios-containers.html">iOS containers</a>
<a href="android-containers.html">Android containers</a>
<a href="project-logs.html">Project logs</a>
<a href="swagger/index.html" target="_blank">Swagger</a>
</div>
<table>
<tr>
Expand Down Expand Up @@ -215,6 +216,10 @@
function getContainerLogs(buttonObj) {
/* Get the container ID that is in the value of the button */
var containerID = buttonObj.value

var url = "/containers/" + containerID + "/logs"
var refreshButton = document.getElementById("refresh-logs-button")
refreshButton.value = url
/* Call the endpoint that will get the container logs */
$.ajax({
dataType: 'JSON',
Expand Down
2 changes: 2 additions & 0 deletions static/project_config.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<link href="/static/css/project_config_styles.css" rel="stylesheet">
<title>Project configuration</title>
</head>

<body>
Expand Down Expand Up @@ -58,6 +59,7 @@
<a href="ios-containers.html">iOS containers</a>
<a href="android-containers.html">Android containers</a>
<a href="project-logs.html">Project logs</a>
<a href="swagger/index.html" target="_blank">Swagger</a>
</div>
<div>
<table id="configurationInfoTable">
Expand Down
Loading

0 comments on commit e07d7b8

Please sign in to comment.