예제 #1
0
func RunTaskFile(task *parser.TaskFile) {
	// Set scripts package settings
	scripts.SetVerbose(verbose)
	scripts.SetDebug(debug)
	scripts.SetDryRun(dryRun)

	os.RemoveAll("tmp")
	os.Mkdir("tmp", 0755)

	fmt.Printf("Running task %s @ %s\n", task.GetMetadata("name"), time.Now().String())

	// If no devices were given, print err and exit
	if len(task.Devices) == 0 {
		fmt.Println("No devices were given in the task file. Exiting.")
		return
	}

	// Load and filter devices
	if verbose {
		fmt.Printf("Loading inventory from %s\n", task.Inventory)
	}
	deviceList, err := devices.ParseFile(task.Inventory)
	if err != nil {
		fmt.Printf("Error loading devices: %s\n", err.Error())
		return
	}

	deviceList, err = devices.Filter(deviceList, task.Devices)
	if err != nil {
		fmt.Printf("Error: %s\n", err.Error())
		return
	}

	// If no devices will be affected, exit
	if len(deviceList.Devices) == 0 {
		fmt.Println("No devices match running task. Exiting.")
		return
	}

	// Compile the script text
	text, err := parser.CompileCommandText(task.DefaultCommandBlock, task)
	if err != nil {
		if parser.IsScriptRun(err) {
			// Run straight script file if prompted
			if err := scripts.ProcessScriptCommand(text, task, deviceList); err != nil {
				fmt.Printf("Error executing task: %s\n", err.Error())
			}
			return
		}
		fmt.Printf("Error compiling script: %s\n", err.Error())
		return
	}

	// Get the template file
	template := task.Template
	if template == "" {
		template = "expect"
	}
	templateFile := "templates/" + template + "-template.tmpl"
	if _, err := os.Stat(templateFile); os.IsNotExist(err) {
		fmt.Printf("Template not found: %s\n", template)
		return
	}

	// Generate an executable script file
	scriptFilename, err := scripts.GenerateBaseScriptFile(templateFile, text, task.GetAllMetadata())
	if err != nil {
		fmt.Printf("Error generating script: %s\n", err.Error())
		return
	}

	if debug {
		fmt.Printf("Script: %s\n", scriptFilename)
	}

	// Execute the script (the dry run setting will stop before actual execution)
	err = scripts.Execute(deviceList, task, scriptFilename, nil)
	if err != nil {
		fmt.Printf("Error executing task: %s\n", err.Error())
		return
	}

	if !debug {
		// Remove base script file
		os.Remove(scriptFilename)
	}

	if dryRun {
		fmt.Print("\nDry Run\n\n")
		for _, host := range deviceList.Devices {
			fmt.Printf("Hostname: %s\n", host.Name)
			fmt.Printf("Address: %s\n", host.GetSetting("address"))
			proto := host.GetSetting("protocol")
			if proto == "" {
				proto = "ssh"
			}
			fmt.Printf("Protocol: %s\n", proto)
			fmt.Println("---------")
		}
	}

	fmt.Printf("\nHosts touched: %d\n", len(deviceList.Devices))
}
예제 #2
0
func main() {
	start := time.Now()
	flag.Parse()

	// Set taskmanager package settings
	taskmanager.SetVerbose(verbose)
	taskmanager.SetDebug(debug)
	taskmanager.SetDryRun(dryRun)

	cliArgs := flag.Args()
	cliArgsc := len(cliArgs)

	// Determine what we're doing
	if cliArgsc == 0 {
		printUsage()
		os.Exit(0)
	}

	if err := checkDependencies(); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	command := cliArgs[0]
	if command == "run" && cliArgsc >= 2 { // Run a task file
		for _, file := range cliArgs[1:] {
			// Parse the task file
			task, err := parser.ParseFile(file)
			if err != nil {
				fmt.Println(err.Error())
				continue
			}
			// Inventory from -i flag, overrides task file
			if inventoryFile != "" {
				task.Inventory = inventoryFile
			}
			if task.Inventory == "" {
				task.Inventory = "devices.conf"
			}
			// Set variables given in the command line into the task
			for k, v := range cliVars {
				task.SetUserData(k, v)
			}
			taskmanager.RunTaskFile(task)
		}
	} else if command == "test" && cliArgsc >= 2 { // Test a task file for errors
		for _, file := range cliArgs[1:] {
			taskmanager.ValidateTaskFile(file)
		}
	} else if command == "version" { // Show version info
		os.Exit(0)
	} else if command == "help" { // Show help info
		printUsage()
		os.Exit(0)
	} else if command == "dev" && cliArgsc == 2 { // Dev stuff
		d, err := devices.ParseFile(cliArgs[1])
		if err != nil {
			fmt.Println(err.Error())
		} else {
			for _, group := range d.Groups {
				fmt.Printf("Group: %s\n", group.Name)
				fmt.Printf("Group Settings: %#v\n", group.GetSettings())
				for _, dev := range group.Devices {
					fmt.Printf("   DeviceName: %s\n", dev.Name)
					fmt.Printf("   Device Settings: %#v\n", dev.GetSettings())
				}
				fmt.Println("")
			}
		}
		os.Exit(0)
	} else {
		printUsage()
		os.Exit(0)
	}

	end := time.Since(start).String()
	fmt.Printf("\nExecution completed in %s\n", end)
}