<commit_before>// Copyright © 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package deploy

import (
	"bytes"
	"context"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"os/exec"
	"path"
	"strings"
	"sync"
	"text/template"

	"github.com/ciao-project/ciao/bat"
	"github.com/pkg/errors"
)

var containerCloudInit = `
---
#cloud-config
runcmd:
    - [ /bin/bash, -c, "while true; do sleep 60; done" ]
...
`

var vmCloudInit = `
---
#cloud-config
users:
  - name: demouser
    gecos: CIAO Demo User
    lock-passwd: false
    sudo: ALL=(ALL) NOPASSWD:ALL
{{- with .Password }}
    passwd: {{ . }}
{{- end }}
{{- with .SSHKey }}
    ssh-authorized-keys:
      - {{ . }}
{{- end }}
...
`

type baseWorkload struct {
	url        string
	imageName  string
	imageID    string
	extra      bool
	localPath  string
	cloudInit  string
	opts       bat.WorkloadOptions
	downloaded bool
	workloadID string
	public     bool
}

type clearWorkload struct {
	wd      baseWorkload
	version string
}

type workloadDetails interface {
	Download(ctx context.Context, imageCacheDir string) error
	Extra() bool
	Upload(ctx context.Context) error
	CreateWorkload(ctx context.Context, sshPublickey string, password string) error
	Cleanup()
}

var images = []workloadDetails{
	&baseWorkload{
		url:       "https://download.fedoraproject.org/pub/fedora/linux/releases/24/CloudImages/x86_64/images/Fedora-Cloud-Base-24-1.2.x86_64.qcow2",
		imageName: "fedora-cloud-base-24-1.2",
		extra:     true,
		cloudInit: vmCloudInit,
		opts: bat.WorkloadOptions{
			Description: "Fedora test VM",
			VMType:      "qemu",
			FWType:      "legacy",
			Requirements: bat.WorkloadRequirements{<commit_msg>ciao-deploy: Bump Fedora release in workload<commit_after>// Copyright © 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package deploy

import (
	"bytes"
	"context"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"os/exec"
	"path"
	"strings"
	"sync"
	"text/template"

	"github.com/ciao-project/ciao/bat"
	"github.com/pkg/errors"
)

var containerCloudInit = `
---
#cloud-config
runcmd:
    - [ /bin/bash, -c, "while true; do sleep 60; done" ]
...
`

var vmCloudInit = `
---
#cloud-config
users:
  - name: demouser
    gecos: CIAO Demo User
    lock-passwd: false
    sudo: ALL=(ALL) NOPASSWD:ALL
{{- with .Password }}
    passwd: {{ . }}
{{- end }}
{{- with .SSHKey }}
    ssh-authorized-keys:
      - {{ . }}
{{- end }}
...
`

type baseWorkload struct {
	url        string
	imageName  string
	imageID    string
	extra      bool
	localPath  string
	cloudInit  string
	opts       bat.WorkloadOptions
	downloaded bool
	workloadID string
	public     bool
}

type clearWorkload struct {
	wd      baseWorkload
	version string
}

type workloadDetails interface {
	Download(ctx context.Context, imageCacheDir string) error
	Extra() bool
	Upload(ctx context.Context) error
	CreateWorkload(ctx context.Context, sshPublickey string, password string) error
	Cleanup()
}

var images = []workloadDetails{
	&baseWorkload{
		url:       "https://download.fedoraproject.org/pub/fedora/linux/releases/27/CloudImages/x86_64/images/Fedora-Cloud-Base-27-1.6.x86_64.qcow2",
		imageName: "fedora-cloud-base-27-1.6",
		extra:     true,
		cloudInit: vmCloudInit,
		opts: bat.WorkloadOptions{
			Description: "Fedora test VM",
			VMType:      "qemu",
			FWType:      "legacy",
			Requirements: bat.WorkloadRequirements{
{'fraction_non_alphanumeric': 0.11940976163450624, 'fraction_numerical': 0.013166855845629965, 'mean_word_length': 3.192197906755471, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4204322', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1457, 'n_words': 424}