Ejemplo n.º 1
0
/*
	Build an agent and add to our list of agents.
*/
func (ad *agent_data) Mk_agent(aid string) (na *agent) {

	na = &agent{}
	na.id = aid
	na.jcache = jsontools.Mk_jsoncache()

	ad.agents[na.id] = na
	ad.build_list()

	return
}
Ejemplo n.º 2
0
func TestJsontools(t *testing.T) {

	var (
		json string
		blob []byte
	)

	json = `{
    "servers": [
        {
            "accessIPv4": "",
            "accessIPv6": "",
            "addresses": {
                "private": [
                    {
                        "addr": "192.168.0.3",
                        "version": 4
                    }
                ]
            },
            "created": "2012-09-07T16:56:37Z",
            "flavor": {
                "id": "1",
                "links": [
                    {
                        "href": "http://openstack.example.com/openstack/flavors/1",
                        "rel": "bookmark"
                    }
                ]
            },
            "hostId": "16d193736a5cfdb60c697ca27ad071d6126fa13baeb670fc9d10645e",
            "id": "05184ba3-00ba-4fbc-b7a2-03b62b884931",
            "image": {
                "id": "70a599e0-31e7-49b7-b260-868f441e862b",
                "links": [
                    {
                        "href": "http://openstack.example.com/openstack/images/70a599e0-31e7-49b7-b260-868f441e862b",
                        "rel": "bookmark"
                    }
                ]
            },
            "links": [
                {
                    "href": "http://openstack.example.com/v2/openstack/servers/05184ba3-00ba-4fbc-b7a2-03b62b884931",
                    "rel": "self"
                },
                {
                    "href": "http://openstack.example.com/openstack/servers/05184ba3-00ba-4fbc-b7a2-03b62b884931",
                    "rel": "bookmark"
                }
            ],
            "metadata": {
                "My Server Name": "Apache1"
            },
            "name": "new-server-test",
            "progress": 0,
            "status": "ACTIVE",
            "tenant_id": "openstack",
            "updated": "2012-09-07T16:56:37Z",
            "user_id": "fake"
        }
    ]
}`
	blob = []byte(json)
	jsontools.Json2map(blob[:], nil, true)

	fmt.Fprintf(os.Stderr, "======================== cache testing =======================\n")
	jc := jsontools.Mk_jsoncache()
	jc.Add_bytes([]byte(`{ "height": "5ft9in",`))
	blob = jc.Get_blob()
	if blob != nil {
		fmt.Fprintf(os.Stderr, " blob wasn't nil and should have been: (%s)\n", string(blob))
		t.Fail()
		os.Exit(1)
	}

	jc.Add_bytes([]byte(` "weight": "100lbs" } { "height", "6ft10in",`))
	blob = jc.Get_blob()
	if blob == nil {
		fmt.Fprintf(os.Stderr, " blob was nil and should NOT have been\n")
		t.Fail()
		os.Exit(1)
	}

	fmt.Fprintf(os.Stderr, "blob was returned as expected: (%s)\n]", string(blob))
	jc.Add_bytes([]byte(` "weight": "190lbs" } { "height", "7ft10in",`))
	blob = jc.Get_blob()
	fmt.Fprintf(os.Stderr, "blob was returned as expected: (%s)\n]", string(blob))

	fmt.Fprintf(os.Stderr, "======================== cache testing # 2 =======================\n")
	jc = jsontools.Mk_jsoncache() // 'clear'
	jc.Add_bytes([]byte(` { "ctype": "action_list", "actions": [ { "atype": "setqueues", "qdata": [ "qos106/fa:de:ad:7a:3a:72,E1res619_00001,2,20000000,20000000,200", "qos102/-128,res619_00001,2,20000000,20000000,200", "qos106/-128,Rres619_00001,2,10000000,10000000,200", "qos102/fa:de:ad:cc:48:f9,E0res619_00001,2,10000000,10000000,200" ], "hosts": [ "qos101", "qos102", "qos103", "qos104", "qos105", "qos106" ] } ] }`))
	blob = jc.Get_blob()
	fmt.Fprintf(os.Stderr, "blob was returned as expected: (%s)\n]", string(blob))

	fmt.Fprintf(os.Stderr, "======================== cache testing # 3 =======================\n")
	jc = jsontools.Mk_jsoncache() // 'clear'
	strs := strings.Split(json, "\n")
	for i := range strs {
		jc.Add_bytes([]byte(strs[i] + "\n"))

		if i < 14 {
			blob = jc.Get_blob()
			if blob != nil {
				fmt.Fprintf(os.Stderr, " blob was NOT nil and should have been: i=%d\n", i)
				t.Fail()
				os.Exit(1)
			}
		}
	}

	blob = jc.Get_blob()
	if blob == nil {
		fmt.Fprintf(os.Stderr, " blob was nil and should NOT have been\n")
		t.Fail()
		os.Exit(1)
	}

	fmt.Fprintf(os.Stderr, "blob was returned as expected: (%s)", string(blob))

}
Ejemplo n.º 3
0
func main() {

	home := os.Getenv("HOME")
	def_user := os.Getenv("LOGNAME")
	def_rdir := "/tmp/tegu_b" // rsync directory created on remote hosts
	def_rlist :=              // list of scripts to copy to remote hosts for execution
		"/usr/bin/create_ovs_queues " +
			"/usr/bin/map_mac2phost " +
			"/usr/bin/ovs_sp2uuid " +
			"/usr/bin/purge_ovs_queues " +
			"/usr/bin/ql_setup_irl " +
			"/usr/bin/ql_setup_ipt " +
			"/usr/bin/send_ovs_fmod " +
			"/usr/bin/tegu_add_mirror " +
			"/usr/bin/tegu_del_mirror " +
			"/usr/bin/ql_bw_fmods " +
			"/usr/bin/ql_bwow_fmods " +
			"/usr/bin/ql_set_trunks " +
			"/usr/bin/ql_filter_rtr " +
			"/usr/bin/setup_ovs_intermed "

	if home == "" {
		home = "/home/tegu" // probably bogus, but we'll have something
	}
	def_key := home + "/.ssh/id_rsa," + home + "/.ssh/id_dsa" // default ssh key to use

	needs_help := flag.Bool("?", false, "show usage") // define recognised command line options
	id := flag.Int("i", 0, "id")
	key_files := flag.String("k", def_key, "ssh-key file(s) for broker")
	log_dir := flag.String("l", "stderr", "log_dir")
	parallel := flag.Int("p", 10, "parallel ssh commands")
	no_rsync := flag.Bool("no-rsync", false, "turn off rsync")
	rdir := flag.String("rdir", def_rdir, "rsync remote directory")
	rlist := flag.String("rlist", def_rlist, "rsync file list")
	tegu_host := flag.String("h", "localhost:29055", "tegu_host:port")
	user := flag.String("u", def_user, "ssh user-name")
	verbose := flag.Bool("v", false, "verbose")
	vlevel := flag.Int("V", 1, "verbose-level")
	flag.Parse() // actually parse the commandline

	if *needs_help {
		usage(version)
		os.Exit(0)
	}

	if *id <= 0 {
		fmt.Fprintf(os.Stderr, "ERR: must enter -i id (number) on command line\n")
		os.Exit(1)
	}

	sheep = bleater.Mk_bleater(0, os.Stderr)
	sheep.Set_prefix(fmt.Sprintf("agent-%d", *id)) // append the pid so that if multiple agents are running they'll use different log files

	if *needs_help {
		usage(version)
		os.Exit(0)
	}

	if *verbose {
		sheep.Set_level(1)
	} else {
		if *vlevel > 0 {
			sheep.Set_level(uint(*vlevel))
		}
	}

	if *log_dir != "stderr" { // allow it to stay on stderr
		lfn := sheep.Mk_logfile_nm(log_dir, 86400)
		sheep.Baa(1, "switching to log file: %s", *lfn)
		sheep.Append_target(*lfn, false)      // switch bleaters to the log file rather than stderr
		go sheep.Sheep_herder(log_dir, 86400) // start the function that will roll the log now and again
	}

	sheep.Baa(1, "tegu_agent %s started", version)
	sheep.Baa(1, "will contact tegu on port: %s", *tegu_host)

	jc := jsontools.Mk_jsoncache()                  // create json cache to buffer tegu datagram input
	sess_mgr := make(chan *connman.Sess_data, 1024) // session management to create tegu connections with and drive the session listener(s)
	smgr := connman.NewManager("", sess_mgr)        // get a manager, but no listen port opened

	connect2tegu(smgr, tegu_host, sess_mgr) // establish initial connection

	ntoks, key_toks := token.Tokenise_populated(*key_files, " ,") // allow space or , seps and drop nil tokens
	if ntoks <= 0 {
		sheep.Baa(0, "CRI: no ssh key files given (-k)")
		os.Exit(1)
	}
	keys := make([]string, ntoks)
	for i := range key_toks {
		keys[i] = key_toks[i]
	}
	broker := ssh_broker.Mk_broker(*user, keys)
	if broker == nil {
		sheep.Baa(0, "CRI: unable to create an ssh broker")
		os.Exit(1)
	}
	if !*no_rsync {
		sheep.Baa(1, "will sync these files to remote hosts: %s", *rlist)
		broker.Add_rsync(rlist, rdir)
	}
	sheep.Baa(1, "successfully created ssh_broker for user: %s, command path: %s", *user, *rdir)
	broker.Start_initiators(*parallel)

	for {
		select { // wait on input from any channel -- just one now, but who knows
		case sreq := <-sess_mgr: // data from the network
			switch sreq.State {
			case connman.ST_ACCEPTED: // shouldn't happen
				sheep.Baa(1, "this shouldn't happen; accepted session????")

			case connman.ST_NEW: // new connection; nothing to process here

			case connman.ST_DISC:
				sheep.Baa(1, "session to tegu was lost")
				connect2tegu(smgr, tegu_host, sess_mgr) // blocks until connected and reports on the conn_ch channel when done
				broker.Reset()                          // reset the broker each time we pick up a new tegu connection

			case connman.ST_DATA:
				sheep.Baa(3, "data: [%s]  %d bytes received", sreq.Id, len(sreq.Buf))
				jc.Add_bytes(sreq.Buf)
				jblob := jc.Get_blob() // get next blob if ready
				for jblob != nil {
					resp := handle_blob(jblob, broker, rdir)
					if resp != nil {
						for i := range resp {
							smgr.Write(sreq.Id, resp[i])
						}
					}

					jblob = jc.Get_blob() // get next blob if more than one in the cache
				}
			}
		} // end select
	}
}