<commit_before>/*
// Copyright (c) 2016 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.
*/

/* TODO

5. Install kernel
12. Make most output from osprepare optional
*/

package main

import (
	"context"
	"flag"
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"syscall"
)

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n\n", os.Args[0])
		fmt.Fprintf(os.Stderr, "%s [prepare|start|stop|quit|status|connect|delete]\n\n", os.Args[0])
		fmt.Fprintln(os.Stderr, "- prepare : creates a new VM")
		fmt.Fprintln(os.Stderr, "- start : boots a stopped VM")
		fmt.Fprintln(os.Stderr, "- stop : cleanly powers down a running VM")
		fmt.Fprintln(os.Stderr, "- quit : quits a running VM")
		fmt.Fprintln(os.Stderr, "- status : prints status information about the ciao-down VM")
		fmt.Fprintln(os.Stderr, "- connect : connects to the VM via SSH")
		fmt.Fprintln(os.Stderr, "- delete : shuts down and deletes the VM")
	}
}

func vmFlags(fs *flag.FlagSet, memGB, CPUs *int) {
	*memGB, *CPUs = getMemAndCpus()
	fs.IntVar(memGB, "mem", *memGB, "Gigabytes of RAM allocated to VM")
	fs.IntVar(CPUs, "cpus", *CPUs, "VCPUs assignged to VM")
}

func prepareFlags() (memGB int, CPUs int, debug bool, err error) {
	fs := flag.NewFlagSet("prepare", flag.ExitOnError)
	vmFlags(fs, &memGB, &CPUs)
	fs.BoolVar(&debug, "debug", false, "Enables debug mode")
	if err := fs.Parse(flag.Args()[1:]); err != nil {
		return -1, -1, false, err
	}

	return memGB, CPUs, debug, nil
}

func startFlags() (memGB int, CPUs int, err error) {
	fs := flag.NewFlagSet("start", flag.ExitOnError)
	vmFlags(fs, &memGB, &CPUs)
	if err := fs.Parse(flag.Args()[1:]); err != nil {
		return -1, -1, err
	}

	return memGB, CPUs, nil
}

func downloadProgress(p progress) {
	if p.totalMB >= 0 {
		fmt.Printf("Downloaded %d MB of %d\n", p.downloadedMB, p.totalMB)
	} else {
		fmt.Printf("Downloaded %d MB\n", p.downloadedMB)
	}
}

func prepare(ctx context.Context, errCh chan error) {
	if !hostSupportsNestedKVM() {
		errCh <- fmt.Errorf("Nested KVM is not enabled.  Please enable try again.")
		return
	}

	fmt.Println("Checking environment")

	memGB, CPUs, debug, err := prepareFlags()
	if err != nil {
		errCh <- err
		return
	}

	ws, err := prepareEnv(ctx)
	if err != nil {
		errCh <- err
		return
	}

	_, err = os.Stat(ws.instanceDir)
	if err == nil {
		errCh <- fmt.Errorf("instance already exists")
		return
	}

	fmt.Println("Installing host dependencies")
	installDeps(ctx)

	err = os.MkdirAll(ws.instanceDir, 0755)
	if err != nil {
		errCh <- fmt.Errorf("unable to create cache dir: %v", err)
		return
	}

	err = prepareSSHKeys(ctx, ws)
	if err != nil {
		errCh <- err
		return
	}

	failed := true
	defer func() {
		if failed {
			_ = os.RemoveAll(ws.instanceDir)
		}
	}()

	qcowPath, err := downloadUbuntu(ctx, ws.ciaoDir, downloadProgress)
	if err != nil {
		errCh <- err
		return
	}

	err = buildISOImage(ctx, ws.instanceDir, ws, debug)
	if err != nil {
		errCh <- err
		return
	}

	err = createRootfs(ctx, qcowPath, ws.instanceDir)
	if err != nil {
		errCh <- err
		return
	}

	fmt.Printf("Booting VM with %d GB RAM and %d cpus\n", memGB, CPUs)

	err = bootVM(ctx, ws, memGB, CPUs)
	if err != nil {
		errCh <- err
		return
	}

	err = manageInstallation(ctx, ws.instanceDir, ws)
	if err != nil {
		errCh <- err
		return
	}
	errCh <- nil
	failed = false
	fmt.Println("VM successfully created!")
	fmt.Println("Type ciao-down connect to start using it.")
}

func start(ctx context.Context, errCh chan error) {
	if !hostSupportsNestedKVM() {
		errCh <- fmt.Errorf("Nested KVM is not enabled.  Please enable try again.")
		return
	}

	memGB, CPUs, err := startFlags()
	if err != nil {
		errCh <- err
		return<commit_msg>ciao-down: Fix golint errors in ciao_down.go<commit_after>/*
// Copyright (c) 2016 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.
*/

/* TODO

5. Install kernel
12. Make most output from osprepare optional
*/

package main

import (
	"context"
	"flag"
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"syscall"
)

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n\n", os.Args[0])
		fmt.Fprintf(os.Stderr, "%s [prepare|start|stop|quit|status|connect|delete]\n\n", os.Args[0])
		fmt.Fprintln(os.Stderr, "- prepare : creates a new VM")
		fmt.Fprintln(os.Stderr, "- start : boots a stopped VM")
		fmt.Fprintln(os.Stderr, "- stop : cleanly powers down a running VM")
		fmt.Fprintln(os.Stderr, "- quit : quits a running VM")
		fmt.Fprintln(os.Stderr, "- status : prints status information about the ciao-down VM")
		fmt.Fprintln(os.Stderr, "- connect : connects to the VM via SSH")
		fmt.Fprintln(os.Stderr, "- delete : shuts down and deletes the VM")
	}
}

func vmFlags(fs *flag.FlagSet, memGB, CPUs *int) {
	*memGB, *CPUs = getMemAndCpus()
	fs.IntVar(memGB, "mem", *memGB, "Gigabytes of RAM allocated to VM")
	fs.IntVar(CPUs, "cpus", *CPUs, "VCPUs assignged to VM")
}

func prepareFlags() (memGB int, CPUs int, debug bool, err error) {
	fs := flag.NewFlagSet("prepare", flag.ExitOnError)
	vmFlags(fs, &memGB, &CPUs)
	fs.BoolVar(&debug, "debug", false, "Enables debug mode")
	if err := fs.Parse(flag.Args()[1:]); err != nil {
		return -1, -1, false, err
	}

	return memGB, CPUs, debug, nil
}

func startFlags() (memGB int, CPUs int, err error) {
	fs := flag.NewFlagSet("start", flag.ExitOnError)
	vmFlags(fs, &memGB, &CPUs)
	if err := fs.Parse(flag.Args()[1:]); err != nil {
		return -1, -1, err
	}

	return memGB, CPUs, nil
}

func downloadProgress(p progress) {
	if p.totalMB >= 0 {
		fmt.Printf("Downloaded %d MB of %d\n", p.downloadedMB, p.totalMB)
	} else {
		fmt.Printf("Downloaded %d MB\n", p.downloadedMB)
	}
}

func prepare(ctx context.Context, errCh chan error) {
	if !hostSupportsNestedKVM() {
		errCh <- fmt.Errorf("nested KVM is not enabled.  Please enable and try again")
		return
	}

	fmt.Println("Checking environment")

	memGB, CPUs, debug, err := prepareFlags()
	if err != nil {
		errCh <- err
		return
	}

	ws, err := prepareEnv(ctx)
	if err != nil {
		errCh <- err
		return
	}

	_, err = os.Stat(ws.instanceDir)
	if err == nil {
		errCh <- fmt.Errorf("instance already exists")
		return
	}

	fmt.Println("Installing host dependencies")
	installDeps(ctx)

	err = os.MkdirAll(ws.instanceDir, 0755)
	if err != nil {
		errCh <- fmt.Errorf("unable to create cache dir: %v", err)
		return
	}

	err = prepareSSHKeys(ctx, ws)
	if err != nil {
		errCh <- err
		return
	}

	failed := true
	defer func() {
		if failed {
			_ = os.RemoveAll(ws.instanceDir)
		}
	}()

	qcowPath, err := downloadUbuntu(ctx, ws.ciaoDir, downloadProgress)
	if err != nil {
		errCh <- err
		return
	}

	err = buildISOImage(ctx, ws.instanceDir, ws, debug)
	if err != nil {
		errCh <- err
		return
	}

	err = createRootfs(ctx, qcowPath, ws.instanceDir)
	if err != nil {
		errCh <- err
		return
	}

	fmt.Printf("Booting VM with %d GB RAM and %d cpus\n", memGB, CPUs)

	err = bootVM(ctx, ws, memGB, CPUs)
	if err != nil {
		errCh <- err
		return
	}

	err = manageInstallation(ctx, ws.instanceDir, ws)
	if err != nil {
		errCh <- err
		return
	}
	errCh <- nil
	failed = false
	fmt.Println("VM successfully created!")
	fmt.Println("Type ciao-down connect to start using it.")
}

func start(ctx context.Context, errCh chan error) {
	if !hostSupportsNestedKVM() {
		errCh <- fmt.Errorf("nested KVM is not enabled.  Please enable and try again")
		return
	}

	memGB, CPUs, err := startFlags()
	if err != nil {
		errCh <- err
		return
{'fraction_non_alphanumeric': 0.14278965395063187, 'fraction_numerical': 0.0056690681469233496, 'mean_word_length': 3.485169491525424, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1663084', 'n_tokens_mistral': 3266, 'n_tokens_neox': 3071, 'n_words': 944}