<commit_before>package main

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"koding/db/models"
	"koding/tools/utils"
	"koding/virt"
	"log"
	"net"
	"os"
	"os/exec"
	"strconv"
	"strings"

	"github.com/jessevdk/go-flags"
	"labix.org/v2/mgo"
	"labix.org/v2/mgo/bson"
)

const (
	usage = `usage: <action> [<vm-id>|all]

	list
	start
	shutdown
	stop
	ip
	unprepare
	create-test-vms
	rbd-orphans
`
)

var flagOpts struct {
	Templates string `long:"templates" short:"t" description:"Change template dir." default:"files/templates"`
}

func main() {
	remainingArgs, err := flags.Parse(&flagOpts)
	if err != nil {
		log.Fatal(err)
	}

	if err := virt.LoadTemplates(flagOpts.Templates); err != nil {
		log.Fatal(err)
	}

	if len(remainingArgs) == 0 {
		fmt.Fprintf(os.Stderr, usage)
		os.Exit(0)
	}

	action := remainingArgs[0]
	actionArgs := remainingArgs[1:]

	fn := actions[action]
	fn(actionArgs)
}

var actions = map[string]func(args []string){
	"list": func(args []string) {
		dirs, err := ioutil.ReadDir("/var/lib/lxc")
		if err != nil {
			log.Println(err)
			return
		}

		for _, dir := range dirs {
			if strings.HasPrefix(dir.Name(), "vm-") {
				fmt.Println(dir.Name())
			}
		}

	},

	"start": func(args []string) {
		for _, vm := range selectVMs(args[0]) {
			err := vm.Start()
			fmt.Printf("%v: %v\n%s", vm, err)
		}
	},

	"shutdown": func(args []string) {
		for _, vm := range selectVMs(args[0]) {
			err := vm.Shutdown()
			fmt.Printf("%v: %v\n%s", vm, err)
		}
	},

	"stop": func(args []string) {
		for _, vm := range selectVMs(args[0]) {
			err := vm.Stop()
			fmt.Printf("%v: %v\n%s", vm, err)
		}
	},

	"unprepare": func(args []string) {
		for _, vm := range selectVMs(args[0]) {
			err := vm.Unprepare()
			fmt.Printf("%v: %v\n", vm, err)
		}
	},

	"ip": func(args []string) {
		if len(args) != 2 {
			log.Fatal("usage: ip <mongo-url> <vm-id>")
		}

		session, err := mgo.Dial(args[0])
		if err != nil {
			panic(err)
		}

		vm := new(models.VM)
		session.SetSafe(&mgo.Safe{})

		vmId := strings.TrimPrefix(args[1], "vm-")

		database := session.DB("")
		err = database.C("jVMs").Find(bson.M{"_id": bson.ObjectIdHex(vmId)}).One(vm)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		fmt.Println(vm.IP.String())
	},

	"create-test-vms": func(args []string) {
		startIP := net.IPv4(10, 128, 2, 7)
		if len(os.Args) >= 4 {
			startIP = net.ParseIP(os.Args[3])
		}
		ipPoolFetch, _ := utils.NewIntPool(utils.IPToInt(startIP), nil)
		count, _ := strconv.Atoi(args[0])
		done := make(chan int)
		for i := 0; i < count; i++ {
			go func(i int) {
				vm := virt.VM{
					Id: bson.NewObjectId(),
					IP: utils.IntToIP(<-ipPoolFetch),
				}
				vm.ApplyDefaults()
				vm.Prepare(false)
				done <- i
			}(i)
		}
		for i := 0; i < count; i++ {
			fmt.Println(<-done)
		}
	},

	"rbd-orphans": func(args []string) {
		if len(args) == 0 {
			log.Fatal("usage: vmtool rbd-orphans <mongo-url>")
		}

		session, err := mgo.Dial(args[0])
		if err != nil {
			panic(err)
		}
		session.SetSafe(&mgo.Safe{})
		database := session.DB("")
		iter := database.C("jVMs").Find(bson.M{}).Select(bson.M{"_id": 1}).Iter()
		var vm struct {
			Id bson.ObjectId `bson:"_id"`
		}
		ids := make(map[string]bool)
		for iter.Next(&vm) {
			ids["vm-"+vm.Id.Hex()] = true
		}
		if err := iter.Close(); err != nil {
			panic(err)
		}

		cmd := exec.Command("/usr/bin/rbd", "ls", "--pool", "vms")<commit_msg>vmtool: start and wait for network for test vms<commit_after>package main

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"koding/db/models"
	"koding/tools/utils"
	"koding/virt"
	"log"
	"net"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"time"

	"github.com/jessevdk/go-flags"
	"labix.org/v2/mgo"
	"labix.org/v2/mgo/bson"
)

const (
	usage = `usage: <action> [<vm-id>|all]

	list
	start
	shutdown
	stop
	ip
	unprepare
	create-test-vms
	rbd-orphans
`
)

var flagOpts struct {
	Templates string `long:"templates" short:"t" description:"Change template dir." default:"files/templates"`
}

func main() {
	remainingArgs, err := flags.Parse(&flagOpts)
	if err != nil {
		log.Fatal(err)
	}

	if err := virt.LoadTemplates(flagOpts.Templates); err != nil {
		log.Fatal(err)
	}

	if len(remainingArgs) == 0 {
		fmt.Fprintf(os.Stderr, usage)
		os.Exit(0)
	}

	action := remainingArgs[0]
	actionArgs := remainingArgs[1:]

	fn := actions[action]
	fn(actionArgs)
}

var actions = map[string]func(args []string){
	"list": func(args []string) {
		dirs, err := ioutil.ReadDir("/var/lib/lxc")
		if err != nil {
			log.Println(err)
			return
		}

		for _, dir := range dirs {
			if strings.HasPrefix(dir.Name(), "vm-") {
				fmt.Println(dir.Name())
			}
		}

	},

	"start": func(args []string) {
		for _, vm := range selectVMs(args[0]) {
			err := vm.Start()
			fmt.Printf("%v: %v\n%s", vm, err)
		}
	},

	"shutdown": func(args []string) {
		for _, vm := range selectVMs(args[0]) {
			err := vm.Shutdown()
			fmt.Printf("%v: %v\n%s", vm, err)
		}
	},

	"stop": func(args []string) {
		for _, vm := range selectVMs(args[0]) {
			err := vm.Stop()
			fmt.Printf("%v: %v\n%s", vm, err)
		}
	},

	"unprepare": func(args []string) {
		for _, vm := range selectVMs(args[0]) {
			err := vm.Unprepare()
			fmt.Printf("%v: %v\n", vm, err)
		}
	},

	"ip": func(args []string) {
		if len(args) != 2 {
			log.Fatal("usage: ip <mongo-url> <vm-id>")
		}

		session, err := mgo.Dial(args[0])
		if err != nil {
			panic(err)
		}

		vm := new(models.VM)
		session.SetSafe(&mgo.Safe{})

		vmId := strings.TrimPrefix(args[1], "vm-")

		database := session.DB("")
		err = database.C("jVMs").Find(bson.M{"_id": bson.ObjectIdHex(vmId)}).One(vm)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		fmt.Println(vm.IP.String())
	},

	"create-test-vms": func(args []string) {
		startIP := net.IPv4(10, 128, 2, 7)
		if len(os.Args) >= 4 {
			startIP = net.ParseIP(os.Args[3])
		}
		ipPoolFetch, _ := utils.NewIntPool(utils.IPToInt(startIP), nil)
		count, _ := strconv.Atoi(args[0])

		done := make(chan string)
		for i := 0; i < count; i++ {
			go func(i int) {
				vm := virt.VM{
					Id: bson.NewObjectId(),
					IP: utils.IntToIP(<-ipPoolFetch),
				}
				vm.ApplyDefaults()
				fmt.Println(i, "preparing...")
				for _ = range vm.Prepare(false) {
				}

				fmt.Println(i, "starting...")
				if err := vm.Start(); err != nil {
					log.Println(i, "start", err)
				}

				// wait until network is up
				fmt.Println(i, "waiting...")
				if err := vm.WaitForNetwork(time.Second * 5); err != nil {
					log.Print(i, "WaitForNetwork", err)
				}
				done <- fmt.Sprintln(i, "ready", "vm-"+vm.Id.Hex())
			}(i)
		}

		for i := 0; i < count; i++ {
			fmt.Println(<-done)
		}
	},

	"rbd-orphans": func(args []string) {
		if len(args) == 0 {
			log.Fatal("usage: vmtool rbd-orphans <mongo-url>")
		}

		session, err := mgo.Dial(args[0])
		if err != nil {
			panic(err)
		}
		session.SetSafe(&mgo.Safe{})
		database := session.DB("")
		iter := database.C("jVMs").Find(bson.M{}).Select(bson.M{"_id": 1}).Iter()
		var vm struct {
			Id bson.ObjectId `bson:"_id"`
		}
		ids := make(map[string]bool)
		for iter.Next(&vm) {
			ids["vm-"+vm.Id.Hex()] = true
		}
		if err := iter.Close(); err != nil {
			panic(err)
		}

		cmd := exec.Command("/usr/bin/rbd", "ls", "--pool", "vms")
{'fraction_non_alphanumeric': 0.21026352288488212, 'fraction_numerical': 0.008460471567267683, 'mean_word_length': 3.0878684807256236, 'pattern_counts': {'":': 20, '<': 23, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4422059', 'n_tokens_mistral': 3446, 'n_tokens_neox': 3071, 'n_words': 588}