Exemplo n.º 1
0
func (v *vm) Machine(id string) (vms.Machine, error) {
	rows, err := db.Query(
		`SELECT id, name, ip, plazaport, username, password
		FROM machines WHERE id = $1::varchar`, id)
	if err != nil {
		fmt.Println(err.Error())
		return nil, err
	}
	machine := &machine{}
	for rows.Next() {
		rows.Scan(
			&machine.id,
			&machine.name,
			&machine.server,
			&machine.plazaport,
			&machine.user,
			&machine.password,
		)
	}
	err = rows.Err()
	if err != nil {
		log.Error(err.Error())
		return nil, err
	}
	return machine, nil
}
Exemplo n.º 2
0
func (m *machine) Status() (vms.MachineStatus, error) {
	var service string
	if m.role == "ad" {
		service = "ADWS"
	} else if m.role == "exec" {
		service = "RDMS"
	}
	cmd := exec.Command(
		"sshpass", "-p", m.password,
		"ssh", "-o", "StrictHostKeyChecking=no",
		"-o", "ConnectTimeout=1",
		"-o", "UserKnownHostsFile=/dev/null",
		"-p", m.sshport,
		fmt.Sprintf(
			"%s@%s",
			m.user,
			m.server,
		),
		"powershell.exe \"Write-Host (Get-Service -Name "+service+").status\"",
	)
	response, err := cmd.CombinedOutput()
	if err != nil {
		log.Error("Failed to check windows' state", err, string(response))
		return vms.StatusUnknown, err
	}
	if strings.Contains(string(response), "Running") {
		return vms.StatusUp, nil
	}
	return vms.StatusDown, nil
}
Exemplo n.º 3
0
func ExecuteCommandAsAdmin(cmd, username, pwd, domain string) {
	out, err := exec.Command(cmd).Output()
	if err != nil {
		log.Error(err)
	}
	log.Info(out)
}
Exemplo n.º 4
0
func ExecuteCommandAsAdmin(cmd, username, pwd, domain string) {
	var si startupinfo
	var handle HANDLE
	var pi processinfo

	si.cb = uint32(unsafe.Sizeof(si))

	a := syscall.MustLoadDLL("advapi32.dll")
	LogonUserW := a.MustFindProc("LogonUserW")
	r1, r2, lastError := LogonUserW.Call(
		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(username))),
		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(domain))),
		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(pwd))),
		LOGON32_LOGON_INTERACTIVE,
		LOGON32_PROVIDER_DEFAULT,
		uintptr(unsafe.Pointer(&handle)),
	)
	log.Error(r1)
	log.Error(r2)
	log.Error(lastError)

	CreateProcessAsUser := a.MustFindProc("CreateProcessAsUserW")
	r1, r2, lastError = CreateProcessAsUser.Call(
		uintptr(unsafe.Pointer(handle)),
		uintptr(unsafe.Pointer(nil)),
		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(cmd))),
		uintptr(unsafe.Pointer(nil)),
		uintptr(unsafe.Pointer(nil)),
		uintptr(0),
		uintptr(unsafe.Pointer(nil)),
		uintptr(unsafe.Pointer(nil)),
		uintptr(unsafe.Pointer(nil)),
		uintptr(unsafe.Pointer(&si)),
		uintptr(unsafe.Pointer(&pi)),
	)
	log.Error(r1)
	log.Error(r2)
	log.Error(lastError)

	b := syscall.MustLoadDLL("Kernel32.dll")
	CloseHandle := b.MustFindProc("CloseHandle")
	r1, r2, lastError = CloseHandle.Call(
		uintptr(unsafe.Pointer(handle)),
	)
	log.Error(r1)
	log.Error(r2)
	log.Error(lastError)
}
Exemplo n.º 5
0
func (m *machine) Status() (vms.MachineStatus, error) {
	resp, err := http.Get("http://" + m.server + ":" + m.plazaport + "/checkrds")
	if err != nil || resp.StatusCode != http.StatusOK {
		log.Error(err)
		return vms.StatusUnknown, nil
	}

	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Error(err)
		return vms.StatusUnknown, nil
	}

	if strings.Contains(string(b), "Running") {
		return vms.StatusUp, nil
	}
	return vms.StatusDown, nil

}
Exemplo n.º 6
0
func realMain() int {

	// reads and parses application config params
	config, err := config.ReadFromFile("./config.json")
	if err != nil {
		fmt.Println("Config file reading error. Details: ", err)
		return 1
	}

	// creates Logger (can be replaced with other popular logger)
	log := log.New("GOB ")
	log.Debug("Application launched")

	// creates Backend object
	back, err := backend.New(&config.Backend, log)
	if err != nil {
		log.Error("Backend initialisation error. Details: ", err)
		return 2
	}

	// creates Frontend objects
	front, err := frontend.New(&config.Frontend, log, back)
	if err != nil {
		log.Error("Frontend initialisation error. Details: ", err)
		return 3
	}

	// starts background processes
	if err := back.Start(); err != nil {
		log.Error("Backend processes launching error. Details: ", err)
		return 4
	}

	// starts HTTP listener and handlers
	if err := front.Start(); err != nil {
		log.Error("Frontend listeners/processes error. Details: ", err)
		return 5
	}

	log.Info("Application succesfully terminated")
	return 0
}
Exemplo n.º 7
0
func TestFiles(t *testing.T) {
	path, _ := filepath.Abs(".")
	s, err := New(path, OutputJson)
	if err != nil {
		t.Fail()
	}
	if s.String() != testData {
		log.Error("Unexpected data")
		t.Fail()
	}
}
Exemplo n.º 8
0
func (m *machine) Status() (vms.MachineStatus, error) {
	resp, err := http.Get("http://" + m.server + ":" + m.plazaport + "/")
	if err != nil {
		log.Error(err)
		return vms.StatusDown, nil
	}

	if resp.StatusCode != http.StatusOK {
		return vms.StatusUnknown, nil
	}

	return vms.StatusUp, nil
}
Exemplo n.º 9
0
func (d *MachineDriver) GetReferencedStructs() []jsonapi.MarshalIdentifier {
	types, err := vms.Types()
	if err != nil {
		log.Error(err)
		return nil
	}

	rt := make([]jsonapi.MarshalIdentifier, 0)

	for _, t := range types {
		rt = append(rt, t)
	}

	return rt
}
Exemplo n.º 10
0
Arquivo: valid.go Projeto: zqzca/back
// ValidateUsername checks to see if the username is in use
func (u Controller) ValidateUsername(e echo.Context) error {
	name := e.Param("name")

	count, err := models.Users(u.DB, qm.Where("username=?", name)).Count()
	if err != nil {
		log.Error("failed to get user from db", "err", err)
		return e.NoContent(http.StatusInternalServerError)
	}

	if count > 0 {
		return e.NoContent(http.StatusNotAcceptable)
	}

	return e.NoContent(http.StatusOK)
}
Exemplo n.º 11
0
func (h *handler) SearchbackHandler(c *echo.Context) error {
	searchq := strings.ToLower(c.Query("search"))
	packages, err := h.back.Model.Package.GetItems(searchq)
	if err != nil {
		log.Error(err)
		return c.Redirect(http.StatusTemporaryRedirect, "/")
	}
	if len(searchq) > 50 && len(searchq) < 4 {
		return c.Redirect(http.StatusBadRequest, "/")
	}
	data := struct {
		Packages []model.PackageRow
	}{
		packages,
	}
	return c.Render(http.StatusOK, "search.html", data)
}
Exemplo n.º 12
0
func (d *MachineDriver) GetReferencedIDs() []jsonapi.ReferenceID {
	types, err := vms.Types()
	if err != nil {
		log.Error(err)
		return nil
	}

	rt := make([]jsonapi.ReferenceID, 0)

	for _, t := range types {

		rt = append(rt, jsonapi.ReferenceID{
			ID:   t.GetID(),
			Type: "machine-types",
			Name: "types",
		})
	}

	return rt
}
Exemplo n.º 13
0
// New creates an instance of Echo.
func New() (e *Echo) {
	e = &Echo{maxParam: new(int)}
	e.pool.New = func() interface{} {
		return NewContext(nil, new(Response), e)
	}
	e.router = NewRouter(e)

	//----------
	// Defaults
	//----------

	e.HTTP2(true)
	e.defaultHTTPErrorHandler = func(err error, c *Context) {
		code := http.StatusInternalServerError
		msg := http.StatusText(code)
		if he, ok := err.(*HTTPError); ok {
			code = he.code
			msg = he.message
		}
		if e.debug {
			msg = err.Error()
		}
		if !c.response.committed {
			http.Error(c.response, msg, code)
		}
		log.Error(err)
	}
	e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler)
	e.SetBinder(&binder{})

	// Logger
	log.SetPrefix("echo")
	log.SetLevel(log.INFO)

	return
}